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;
425 /* Pass1 : calculate the buffer size required */
427 for (sec = cfg->sections; NULL != sec; sec = sec->next)
429 /* For each section we need to add 3 charaters: {'[',']','\n'} */
430 m_size += strlen (sec->name) + 3;
431 for (ent = sec->entries; NULL != ent; ent = ent->next)
433 if (NULL != ent->val)
435 /* if val has any '\n' then they occupy +1 character as '\n'->'\\','n' */
437 while (NULL != (pos = strstr (pos, "\n")))
442 /* For each key = value pair we need to add 4 characters (2
443 spaces and 1 equal-to character and 1 new line) */
444 m_size += strlen (ent->key) + strlen (ent->val) + 4;
447 /* A new line after section end */
451 /* Pass2: Allocate memory and write the configuration to it */
452 mem = GNUNET_malloc (m_size);
458 len = GNUNET_asprintf (&cbuf, "[%s]\n", sec->name);
459 GNUNET_assert (0 < len);
460 GNUNET_memcpy (mem + c_size, cbuf, len);
463 for (ent = sec->entries; NULL != ent; ent = ent->next)
465 if (NULL != ent->val)
467 val = GNUNET_malloc (strlen (ent->val) * 2 + 1);
468 strcpy (val, ent->val);
469 while (NULL != (pos = strstr (val, "\n")))
471 memmove (&pos[2], &pos[1], strlen (&pos[1]));
475 len = GNUNET_asprintf (&cbuf, "%s = %s\n", ent->key, val);
477 GNUNET_memcpy (mem + c_size, cbuf, len);
482 GNUNET_memcpy (mem + c_size, "\n", 1);
486 GNUNET_assert (c_size == m_size);
493 * Write configuration file.
495 * @param cfg configuration to write
496 * @param filename where to write the configuration
497 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
500 GNUNET_CONFIGURATION_write (struct GNUNET_CONFIGURATION_Handle *cfg,
501 const char *filename)
508 fn = GNUNET_STRINGS_filename_expand (filename);
510 return GNUNET_SYSERR;
511 if (GNUNET_OK != GNUNET_DISK_directory_create_for_file (fn))
514 return GNUNET_SYSERR;
516 cfg_buf = GNUNET_CONFIGURATION_serialize (cfg, &size);
517 sret = GNUNET_DISK_fn_write (fn, cfg_buf, size,
518 GNUNET_DISK_PERM_USER_READ
519 | GNUNET_DISK_PERM_USER_WRITE
520 | GNUNET_DISK_PERM_GROUP_READ
521 | GNUNET_DISK_PERM_GROUP_WRITE);
523 (size != (size_t) sret) )
526 GNUNET_free (cfg_buf);
527 LOG (GNUNET_ERROR_TYPE_WARNING,
528 "Writing configration to file `%s' failed\n",
530 cfg->dirty = GNUNET_SYSERR; /* last write failed */
531 return GNUNET_SYSERR;
534 GNUNET_free (cfg_buf);
535 cfg->dirty = GNUNET_NO; /* last write succeeded */
541 * Iterate over all options in the configuration.
543 * @param cfg configuration to inspect
544 * @param iter function to call on each option
545 * @param iter_cls closure for @a iter
548 GNUNET_CONFIGURATION_iterate (const struct GNUNET_CONFIGURATION_Handle *cfg,
549 GNUNET_CONFIGURATION_Iterator iter,
552 struct ConfigSection *spos;
553 struct ConfigEntry *epos;
555 for (spos = cfg->sections; NULL != spos; spos = spos->next)
556 for (epos = spos->entries; NULL != epos; epos = epos->next)
557 if (NULL != epos->val)
558 iter (iter_cls, spos->name, epos->key, epos->val);
563 * Iterate over values of a section in the configuration.
565 * @param cfg configuration to inspect
566 * @param section the section
567 * @param iter function to call on each option
568 * @param iter_cls closure for @a iter
571 GNUNET_CONFIGURATION_iterate_section_values (const struct
572 GNUNET_CONFIGURATION_Handle *cfg,
574 GNUNET_CONFIGURATION_Iterator iter,
577 struct ConfigSection *spos;
578 struct ConfigEntry *epos;
580 spos = cfg->sections;
581 while ((spos != NULL) && (0 != strcasecmp (spos->name, section)))
585 for (epos = spos->entries; NULL != epos; epos = epos->next)
586 if (NULL != epos->val)
587 iter (iter_cls, spos->name, epos->key, epos->val);
592 * Iterate over all sections in the configuration.
594 * @param cfg configuration to inspect
595 * @param iter function to call on each section
596 * @param iter_cls closure for @a iter
599 GNUNET_CONFIGURATION_iterate_sections (const struct GNUNET_CONFIGURATION_Handle *cfg,
600 GNUNET_CONFIGURATION_Section_Iterator iter,
603 struct ConfigSection *spos;
604 struct ConfigSection *next;
606 next = cfg->sections;
611 iter (iter_cls, spos->name);
617 * Remove the given section and all options in it.
619 * @param cfg configuration to inspect
620 * @param section name of the section to remove
623 GNUNET_CONFIGURATION_remove_section (struct GNUNET_CONFIGURATION_Handle *cfg,
626 struct ConfigSection *spos;
627 struct ConfigSection *prev;
628 struct ConfigEntry *ent;
631 spos = cfg->sections;
634 if (0 == strcasecmp (section, spos->name))
637 cfg->sections = spos->next;
639 prev->next = spos->next;
640 while (NULL != (ent = spos->entries))
642 spos->entries = ent->next;
643 GNUNET_free (ent->key);
644 GNUNET_free_non_null (ent->val);
646 cfg->dirty = GNUNET_YES;
648 GNUNET_free (spos->name);
659 * Copy a configuration value to the given target configuration.
660 * Overwrites existing entries.
662 * @param cls the destination configuration (`struct GNUNET_CONFIGURATION_Handle *`)
663 * @param section section for the value
664 * @param option option name of the value
665 * @param value value to copy
668 copy_entry (void *cls,
673 struct GNUNET_CONFIGURATION_Handle *dst = cls;
675 GNUNET_CONFIGURATION_set_value_string (dst, section, option, value);
680 * Duplicate an existing configuration object.
682 * @param cfg configuration to duplicate
683 * @return duplicate configuration
685 struct GNUNET_CONFIGURATION_Handle *
686 GNUNET_CONFIGURATION_dup (const struct GNUNET_CONFIGURATION_Handle *cfg)
688 struct GNUNET_CONFIGURATION_Handle *ret;
690 ret = GNUNET_CONFIGURATION_create ();
691 GNUNET_CONFIGURATION_iterate (cfg, ©_entry, ret);
697 * Find a section entry from a configuration.
699 * @param cfg configuration to search in
700 * @param section name of the section to look for
701 * @return matching entry, NULL if not found
703 static struct ConfigSection *
704 find_section (const struct GNUNET_CONFIGURATION_Handle *cfg,
707 struct ConfigSection *pos;
710 while ((pos != NULL) && (0 != strcasecmp (section, pos->name)))
717 * Find an entry from a configuration.
719 * @param cfg handle to the configuration
720 * @param section section the option is in
721 * @param key the option
722 * @return matching entry, NULL if not found
724 static struct ConfigEntry *
725 find_entry (const struct GNUNET_CONFIGURATION_Handle *cfg,
729 struct ConfigSection *sec;
730 struct ConfigEntry *pos;
732 if (NULL == (sec = find_section (cfg, section)))
735 while ((pos != NULL) && (0 != strcasecmp (key, pos->key)))
742 * A callback function, compares entries from two configurations
743 * (default against a new configuration) and write the diffs in a
744 * diff-configuration object (the callback object).
746 * @param cls the diff configuration (`struct DiffHandle *`)
747 * @param section section for the value (of the default conf.)
748 * @param option option name of the value (of the default conf.)
749 * @param value value to copy (of the default conf.)
752 compare_entries (void *cls,
757 struct DiffHandle *dh = cls;
758 struct ConfigEntry *entNew;
760 entNew = find_entry (dh->cfg_default, section, option);
761 if ( (NULL != entNew) &&
762 (NULL != entNew->val) &&
763 (0 == strcmp (entNew->val, value)) )
765 GNUNET_CONFIGURATION_set_value_string (dh->cfgDiff, section, option, value);
770 * Compute configuration with only entries that have been changed
772 * @param cfg_default original configuration
773 * @param cfg_new new configuration
774 * @return configuration with only the differences, never NULL
776 struct GNUNET_CONFIGURATION_Handle *
777 GNUNET_CONFIGURATION_get_diff (const struct GNUNET_CONFIGURATION_Handle *cfg_default,
778 const struct GNUNET_CONFIGURATION_Handle *cfg_new)
780 struct DiffHandle diffHandle;
782 diffHandle.cfgDiff = GNUNET_CONFIGURATION_create ();
783 diffHandle.cfg_default = cfg_default;
784 GNUNET_CONFIGURATION_iterate (cfg_new, &compare_entries, &diffHandle);
785 return diffHandle.cfgDiff;
790 * Write only configuration entries that have been changed to configuration file
792 * @param cfg_default default configuration
793 * @param cfg_new new configuration
794 * @param filename where to write the configuration diff between default and new
795 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
798 GNUNET_CONFIGURATION_write_diffs (const struct GNUNET_CONFIGURATION_Handle
800 const struct GNUNET_CONFIGURATION_Handle
801 *cfg_new, const char *filename)
804 struct GNUNET_CONFIGURATION_Handle *diff;
806 diff = GNUNET_CONFIGURATION_get_diff (cfg_default, cfg_new);
807 ret = GNUNET_CONFIGURATION_write (diff, filename);
808 GNUNET_CONFIGURATION_destroy (diff);
814 * Set a configuration value that should be a string.
816 * @param cfg configuration to update
817 * @param section section of interest
818 * @param option option of interest
819 * @param value value to set
822 GNUNET_CONFIGURATION_set_value_string (struct GNUNET_CONFIGURATION_Handle *cfg,
823 const char *section, const char *option,
826 struct ConfigSection *sec;
827 struct ConfigEntry *e;
830 e = find_entry (cfg, section, option);
835 GNUNET_free_non_null (e->val);
840 nv = GNUNET_strdup (value);
841 GNUNET_free_non_null (e->val);
846 sec = find_section (cfg, section);
849 sec = GNUNET_new (struct ConfigSection);
850 sec->name = GNUNET_strdup (section);
851 sec->next = cfg->sections;
854 e = GNUNET_new (struct ConfigEntry);
855 e->key = GNUNET_strdup (option);
856 e->val = GNUNET_strdup (value);
857 e->next = sec->entries;
863 * Set a configuration value that should be a number.
865 * @param cfg configuration to update
866 * @param section section of interest
867 * @param option option of interest
868 * @param number value to set
871 GNUNET_CONFIGURATION_set_value_number (struct GNUNET_CONFIGURATION_Handle *cfg,
874 unsigned long long number)
882 GNUNET_CONFIGURATION_set_value_string (cfg,
890 * Get a configuration value that should be a number.
892 * @param cfg configuration to inspect
893 * @param section section of interest
894 * @param option option of interest
895 * @param number where to store the numeric value of the option
896 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
899 GNUNET_CONFIGURATION_get_value_number (const struct GNUNET_CONFIGURATION_Handle
900 *cfg, const char *section,
902 unsigned long long *number)
904 struct ConfigEntry *e;
906 if (NULL == (e = find_entry (cfg, section, option)))
907 return GNUNET_SYSERR;
909 return GNUNET_SYSERR;
910 if (1 != SSCANF (e->val, "%llu", number))
911 return GNUNET_SYSERR;
916 * Get a configuration value that should be a floating point number.
918 * @param cfg configuration to inspect
919 * @param section section of interest
920 * @param option option of interest
921 * @param number where to store the floating value of the option
922 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
925 GNUNET_CONFIGURATION_get_value_float (const struct GNUNET_CONFIGURATION_Handle
926 *cfg, const char *section,
930 struct ConfigEntry *e;
932 if (NULL == (e = find_entry (cfg, section, option)))
933 return GNUNET_SYSERR;
935 return GNUNET_SYSERR;
936 if (1 != SSCANF (e->val, "%f", number))
937 return GNUNET_SYSERR;
944 * Get a configuration value that should be a relative time.
946 * @param cfg configuration to inspect
947 * @param section section of interest
948 * @param option option of interest
949 * @param time set to the time value stored in the configuration
950 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
953 GNUNET_CONFIGURATION_get_value_time (const struct GNUNET_CONFIGURATION_Handle *cfg,
956 struct GNUNET_TIME_Relative *time)
958 struct ConfigEntry *e;
960 if (NULL == (e = find_entry (cfg, section, option)))
961 return GNUNET_SYSERR;
963 return GNUNET_SYSERR;
964 return GNUNET_STRINGS_fancy_time_to_relative (e->val, time);
969 * Get a configuration value that should be a size in bytes.
971 * @param cfg configuration to inspect
972 * @param section section of interest
973 * @param option option of interest
974 * @param size set to the size in bytes as stored in the configuration
975 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
978 GNUNET_CONFIGURATION_get_value_size (const struct GNUNET_CONFIGURATION_Handle *cfg,
981 unsigned long long *size)
983 struct ConfigEntry *e;
985 if (NULL == (e = find_entry (cfg, section, option)))
986 return GNUNET_SYSERR;
988 return GNUNET_SYSERR;
989 return GNUNET_STRINGS_fancy_size_to_bytes (e->val, size);
994 * Get a configuration value that should be a string.
996 * @param cfg configuration to inspect
997 * @param section section of interest
998 * @param option option of interest
999 * @param value will be set to a freshly allocated configuration
1000 * value, or NULL if option is not specified
1001 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
1004 GNUNET_CONFIGURATION_get_value_string (const struct GNUNET_CONFIGURATION_Handle *cfg,
1005 const char *section,
1009 struct ConfigEntry *e;
1011 if ( (NULL == (e = find_entry (cfg, section, option))) ||
1015 return GNUNET_SYSERR;
1017 *value = GNUNET_strdup (e->val);
1023 * Get a configuration value that should be in a set of
1024 * predefined strings
1026 * @param cfg configuration to inspect
1027 * @param section section of interest
1028 * @param option option of interest
1029 * @param choices NULL-terminated list of legal values
1030 * @param value will be set to an entry in the legal list,
1031 * or NULL if option is not specified and no default given
1032 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
1035 GNUNET_CONFIGURATION_get_value_choice (const struct GNUNET_CONFIGURATION_Handle *cfg,
1036 const char *section,
1038 const char *const *choices,
1041 struct ConfigEntry *e;
1044 if (NULL == (e = find_entry (cfg, section, option)))
1045 return GNUNET_SYSERR;
1046 for (i = 0; NULL != choices[i]; i++)
1047 if (0 == strcasecmp (choices[i], e->val))
1049 if (NULL == choices[i])
1051 LOG (GNUNET_ERROR_TYPE_ERROR,
1052 _("Configuration value '%s' for '%s'"
1053 " in section '%s' is not in set of legal choices\n"),
1057 return GNUNET_SYSERR;
1059 *value = choices[i];
1065 * Get crockford32-encoded fixed-size binary data from a configuration.
1067 * @param cfg configuration to access
1068 * @param section section to access
1069 * @param option option to access
1070 * @param buf where to store the decoded binary result
1071 * @param buf_size exact number of bytes to store in @a buf
1072 * @return #GNUNET_OK on success
1073 * #GNUNET_NO is the value does not exist
1074 * #GNUNET_SYSERR on decoding error
1077 GNUNET_CONFIGURATION_get_data (const struct GNUNET_CONFIGURATION_Handle *cfg,
1078 const char *section,
1088 (res = GNUNET_CONFIGURATION_get_value_string (cfg,
1093 data_size = (strlen (enc) * 5) / 8;
1094 if (data_size != buf_size)
1097 return GNUNET_SYSERR;
1100 GNUNET_STRINGS_string_to_data (enc,
1105 return GNUNET_SYSERR;
1113 * Test if we have a value for a particular option
1115 * @param cfg configuration to inspect
1116 * @param section section of interest
1117 * @param option option of interest
1118 * @return #GNUNET_YES if so, #GNUNET_NO if not.
1121 GNUNET_CONFIGURATION_have_value (const struct GNUNET_CONFIGURATION_Handle *cfg,
1122 const char *section,
1125 struct ConfigEntry *e;
1127 if ((NULL == (e = find_entry (cfg, section, option))) || (NULL == e->val))
1134 * Expand an expression of the form "$FOO/BAR" to "DIRECTORY/BAR"
1135 * where either in the "PATHS" section or the environtment "FOO" is
1136 * set to "DIRECTORY". We also support default expansion,
1137 * i.e. ${VARIABLE:-default} will expand to $VARIABLE if VARIABLE is
1138 * set in PATHS or the environment, and otherwise to "default". Note
1139 * that "default" itself can also be a $-expression, thus
1140 * "${VAR1:-{$VAR2}}" will expand to VAR1 and if that is not defined
1143 * @param cfg configuration to use for path expansion
1144 * @param orig string to $-expand (will be freed!)
1145 * @param depth recursion depth, used to detect recursive expansions
1146 * @return $-expanded string
1149 expand_dollar (const struct GNUNET_CONFIGURATION_Handle *cfg,
1170 LOG (GNUNET_ERROR_TYPE_WARNING,
1171 _("Recursive expansion suspected, aborting $-expansion for term `%s'\n"),
1175 LOG (GNUNET_ERROR_TYPE_DEBUG,
1176 "Asked to $-expand %s\n",
1180 LOG (GNUNET_ERROR_TYPE_DEBUG,
1181 "Doesn't start with $ - not expanding\n");
1203 LOG (GNUNET_ERROR_TYPE_WARNING,
1204 _("Missing closing `%s' in option `%s'\n"),
1216 def = strchr (orig, ':');
1221 if ( ('-' == *def) ||
1224 def = GNUNET_strdup (def);
1232 while ( (orig[i] != '/') &&
1233 (orig[i] != '\\') &&
1234 (orig[i] != '\0') &&
1237 if (orig[i] == '\0')
1243 erased_char = orig[i];
1244 erased_pos = &orig[i];
1246 post = &orig[i + 1];
1249 LOG (GNUNET_ERROR_TYPE_DEBUG,
1250 "Split into `%s' and `%s' with default %s\n",
1255 GNUNET_CONFIGURATION_get_value_string (cfg,
1260 if (NULL == (env = getenv (start)))
1263 def = expand_dollar (cfg, def, depth + 1);
1268 start = GNUNET_strdup (start);
1270 *erased_pos = erased_char;
1271 LOG (GNUNET_ERROR_TYPE_WARNING,
1272 _("Failed to expand `%s' in `%s' as it is neither found in [PATHS] nor defined as an environmental variable\n"),
1274 GNUNET_free (start);
1277 prefix = GNUNET_strdup (env);
1279 prefix = GNUNET_CONFIGURATION_expand_dollar (cfg, prefix);
1280 if ( (erased_pos) && ('}' != erased_char) )
1282 len = strlen (prefix) + 1;
1283 prefix = GNUNET_realloc (prefix, len + 1);
1284 prefix[len - 1] = erased_char;
1287 result = GNUNET_malloc (strlen (prefix) + strlen (post) + 1);
1288 strcpy (result, prefix);
1289 strcat (result, post);
1290 GNUNET_free_non_null (def);
1291 GNUNET_free (prefix);
1298 * Expand an expression of the form "$FOO/BAR" to "DIRECTORY/BAR"
1299 * where either in the "PATHS" section or the environtment "FOO" is
1300 * set to "DIRECTORY". We also support default expansion,
1301 * i.e. ${VARIABLE:-default} will expand to $VARIABLE if VARIABLE is
1302 * set in PATHS or the environment, and otherwise to "default". Note
1303 * that "default" itself can also be a $-expression, thus
1304 * "${VAR1:-{$VAR2}}" will expand to VAR1 and if that is not defined
1307 * @param cfg configuration to use for path expansion
1308 * @param orig string to $-expand (will be freed!). Note that multiple
1309 * $-expressions can be present in this string. They will all be
1311 * @return $-expanded string
1314 GNUNET_CONFIGURATION_expand_dollar (const struct GNUNET_CONFIGURATION_Handle *cfg,
1321 for (i = 0; '\0' != orig[i]; i++)
1325 dup = GNUNET_strdup (orig + i);
1326 dup = expand_dollar (cfg, dup, 0);
1327 len = strlen (dup) + 1;
1328 orig = GNUNET_realloc (orig, i + len);
1329 GNUNET_memcpy (orig + i, dup, len);
1337 * Get a configuration value that should be a string.
1339 * @param cfg configuration to inspect
1340 * @param section section of interest
1341 * @param option option of interest
1342 * @param value will be set to a freshly allocated configuration
1343 * value, or NULL if option is not specified
1344 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
1347 GNUNET_CONFIGURATION_get_value_filename (const struct GNUNET_CONFIGURATION_Handle *cfg,
1348 const char *section,
1355 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &tmp))
1357 LOG (GNUNET_ERROR_TYPE_DEBUG,
1358 "Failed to retrieve filename\n");
1360 return GNUNET_SYSERR;
1362 tmp = GNUNET_CONFIGURATION_expand_dollar (cfg, tmp);
1363 *value = GNUNET_STRINGS_filename_expand (tmp);
1366 return GNUNET_SYSERR;
1372 * Get a configuration value that should be in a set of
1375 * @param cfg configuration to inspect
1376 * @param section section of interest
1377 * @param option option of interest
1378 * @return #GNUNET_YES, #GNUNET_NO or #GNUNET_SYSERR
1381 GNUNET_CONFIGURATION_get_value_yesno (const struct GNUNET_CONFIGURATION_Handle *cfg,
1382 const char *section,
1385 static const char *yesno[] = { "YES", "NO", NULL };
1390 GNUNET_CONFIGURATION_get_value_choice (cfg, section, option, yesno, &val);
1391 if (ret == GNUNET_SYSERR)
1393 if (val == yesno[0])
1400 * Iterate over the set of filenames stored in a configuration value.
1402 * @param cfg configuration to inspect
1403 * @param section section of interest
1404 * @param option option of interest
1405 * @param cb function to call on each filename
1406 * @param cb_cls closure for @a cb
1407 * @return number of filenames iterated over, -1 on error
1410 GNUNET_CONFIGURATION_iterate_value_filenames (const struct GNUNET_CONFIGURATION_Handle *cfg,
1411 const char *section,
1413 GNUNET_FileNameCallback cb,
1423 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &list))
1425 GNUNET_assert (list != NULL);
1430 while (pos[0] == ' ')
1432 if (strlen (pos) == 0)
1435 while ((end[0] != ' ') && (end[0] != '\0'))
1443 memmove (end, &end[1], strlen (&end[1]) + 1);
1445 /* illegal, but just keep it */
1448 /* illegal, but just ignore that there was a '/' */
1456 if (strlen (pos) > 0)
1459 if ((cb != NULL) && (GNUNET_OK != cb (cb_cls, pos)))
1461 ret = GNUNET_SYSERR;
1477 * @param value FIXME
1481 escape_name (const char *value)
1487 escaped = GNUNET_malloc (strlen (value) * 2 + 1);
1488 memset (escaped, 0, strlen (value) * 2 + 1);
1491 while (rpos[0] != '\0')
1514 * @param cls string we compare with (const char*)
1515 * @param fn filename we are currently looking at
1516 * @return #GNUNET_OK if the names do not match, #GNUNET_SYSERR if they do
1519 test_match (void *cls, const char *fn)
1521 const char *of = cls;
1523 return (0 == strcmp (of, fn)) ? GNUNET_SYSERR : GNUNET_OK;
1528 * Append a filename to a configuration value that
1529 * represents a list of filenames
1531 * @param cfg configuration to update
1532 * @param section section of interest
1533 * @param option option of interest
1534 * @param value filename to append
1535 * @return #GNUNET_OK on success,
1536 * #GNUNET_NO if the filename already in the list
1537 * #GNUNET_SYSERR on error
1540 GNUNET_CONFIGURATION_append_value_filename (struct GNUNET_CONFIGURATION_Handle *cfg,
1541 const char *section,
1549 if (GNUNET_SYSERR ==
1550 GNUNET_CONFIGURATION_iterate_value_filenames (cfg, section, option,
1553 return GNUNET_NO; /* already exists */
1555 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &old))
1556 old = GNUNET_strdup ("");
1557 escaped = escape_name (value);
1558 nw = GNUNET_malloc (strlen (old) + strlen (escaped) + 2);
1560 if (strlen (old) > 0)
1562 strcat (nw, escaped);
1563 GNUNET_CONFIGURATION_set_value_string (cfg, section, option, nw);
1566 GNUNET_free (escaped);
1572 * Remove a filename from a configuration value that
1573 * represents a list of filenames
1575 * @param cfg configuration to update
1576 * @param section section of interest
1577 * @param option option of interest
1578 * @param value filename to remove
1579 * @return #GNUNET_OK on success,
1580 * #GNUNET_NO if the filename is not in the list,
1581 * #GNUNET_SYSERR on error
1584 GNUNET_CONFIGURATION_remove_value_filename (struct GNUNET_CONFIGURATION_Handle
1585 *cfg, const char *section,
1596 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &list))
1598 match = escape_name (value);
1602 while (pos[0] == ' ')
1604 if (strlen (pos) == 0)
1607 while ((end[0] != ' ') && (end[0] != '\0'))
1618 /* illegal, but just keep it */
1621 /* illegal, but just ignore that there was a '/' */
1629 if (0 == strcmp (pos, match))
1632 memmove (pos, &end[1], strlen (&end[1]) + 1);
1640 GNUNET_CONFIGURATION_set_value_string (cfg, section, option, list);
1642 GNUNET_free (match);
1651 GNUNET_free (match);
1657 * Wrapper around #GNUNET_CONFIGURATION_parse. Called on each
1658 * file in a directory, we trigger parsing on those files that
1661 * @param cls the cfg
1662 * @param filename file to parse
1663 * @return #GNUNET_OK on success
1666 parse_configuration_file (void *cls, const char *filename)
1668 struct GNUNET_CONFIGURATION_Handle *cfg = cls;
1672 /* Examine file extension */
1673 ext = strrchr (filename, '.');
1674 if ((NULL == ext) || (0 != strcmp (ext, ".conf")))
1676 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1677 "Skipping file `%s'\n",
1682 ret = GNUNET_CONFIGURATION_parse (cfg, filename);
1688 * Load default configuration. This function will parse the
1689 * defaults from the given defaults_d directory.
1691 * @param cfg configuration to update
1692 * @param defaults_d directory with the defaults
1693 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
1696 GNUNET_CONFIGURATION_load_from (struct GNUNET_CONFIGURATION_Handle *cfg,
1697 const char *defaults_d)
1699 if (GNUNET_SYSERR ==
1700 GNUNET_DISK_directory_scan (defaults_d, &parse_configuration_file, cfg))
1701 return GNUNET_SYSERR; /* no configuration at all found */
1706 /* end of configuration.c */