2 This file is part of GNUnet.
3 (C) 2006, 2007, 2008, 2009, 2013 Christian Grothoff (and other contributing authors)
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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file src/util/configuration.c
23 * @brief configuration management
24 * @author Christian Grothoff
28 #include "gnunet_util_lib.h"
30 #define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
32 #define LOG_STRERROR_FILE(kind,syscall,filename) GNUNET_log_from_strerror_file (kind, "util", syscall, filename)
35 * @brief configuration entry
41 * This is a linked list.
43 struct ConfigEntry *next;
51 * current, commited value
58 * @brief configuration section
63 * This is a linked list.
65 struct ConfigSection *next;
68 * entries in the section
70 struct ConfigEntry *entries;
80 * @brief configuration data
82 struct GNUNET_CONFIGURATION_Handle
85 * Configuration sections.
87 struct ConfigSection *sections;
90 * Modification indication since last save
91 * GNUNET_NO if clean, GNUNET_YES if dirty,
92 * GNUNET_SYSERR on error (i.e. last save failed)
100 * Used for diffing a configuration object against
105 const struct GNUNET_CONFIGURATION_Handle *cfgDefault;
106 struct GNUNET_CONFIGURATION_Handle *cfgDiff;
112 * Create a GNUNET_CONFIGURATION_Handle.
114 * @return fresh configuration object
116 struct GNUNET_CONFIGURATION_Handle *
117 GNUNET_CONFIGURATION_create ()
119 return GNUNET_new (struct GNUNET_CONFIGURATION_Handle);
124 * Destroy configuration object.
126 * @param cfg configuration to destroy
129 GNUNET_CONFIGURATION_destroy (struct GNUNET_CONFIGURATION_Handle *cfg)
131 struct ConfigSection *sec;
133 while (NULL != (sec = cfg->sections))
134 GNUNET_CONFIGURATION_remove_section (cfg, sec->name);
140 * De-serializes configuration
142 * @param cfg configuration to update
143 * @param mem the memory block of serialized configuration
144 * @param size the size of the memory block
145 * @param allow_inline set to GNUNET_YES if we recursively load configuration
146 * from inlined configurations; GNUNET_NO if not and raise warnings
147 * when we come across them
148 * @return GNUNET_OK on success, GNUNET_ERROR on error
151 GNUNET_CONFIGURATION_deserialize (struct GNUNET_CONFIGURATION_Handle *cfg,
171 LOG (GNUNET_ERROR_TYPE_DEBUG, "Deserializing config file\n");
173 section = GNUNET_strdup ("");
177 while (r_bytes < size)
179 GNUNET_free_non_null (line_orig);
180 /* fgets-like behaviour on buffer */
181 to_read = size - r_bytes;
182 pos = memchr (&mem[r_bytes], '\n', to_read);
185 line_orig = GNUNET_strndup (&mem[r_bytes], line_size = to_read);
186 r_bytes += line_size;
190 line_orig = GNUNET_strndup (&mem[r_bytes], line_size = (pos - &mem[r_bytes]));
191 r_bytes += line_size + 1;
194 /* increment line number */
196 /* tabs and '\r' are whitespace */
197 emptyline = GNUNET_YES;
198 for (i = 0; i < line_size; i++)
205 emptyline = GNUNET_NO;
207 /* ignore empty lines */
208 if (GNUNET_YES == emptyline)
211 /* remove tailing whitespace */
212 for (i = line_size - 1; (i >= 1) && (isspace ((unsigned char) line[i]));i--)
215 /* remove leading whitespace */
216 for (; line[0] != '\0' && (isspace ((unsigned char) line[0])); line++);
218 /* ignore comments */
219 if ( ('#' == line[0]) || ('%' == line[0]) )
222 /* handle special "@INLINE@" directive */
223 if (0 == strncasecmp (line,
225 strlen ("@INLINE@ ")))
228 value = &line[strlen ("@INLINE@ ")];
229 if (GNUNET_YES == allow_inline)
231 if (GNUNET_OK != GNUNET_CONFIGURATION_parse (cfg, value))
233 ret = GNUNET_SYSERR; /* failed to parse included config */
239 LOG (GNUNET_ERROR_TYPE_DEBUG,
240 "Ignoring parsing @INLINE@ configurations, not allowed!\n");
246 if ( ('[' == line[0]) && (']' == line[line_size - 1]) )
249 line[line_size - 1] = '\0';
251 GNUNET_free (section);
252 section = GNUNET_strdup (value);
253 LOG (GNUNET_ERROR_TYPE_DEBUG,
254 "Config section `%s'\n",
258 if (NULL != (eq = strchr (line, '=')))
261 tag = GNUNET_strndup (line, eq - line);
262 /* remove tailing whitespace */
263 for (i = strlen (tag) - 1; (i >= 1) && (isspace ((unsigned char) tag[i]));i--)
266 /* Strip whitespace */
268 while (isspace ((unsigned char) value[0]))
270 for (i = strlen (value) - 1; (i >= 1) && (isspace ((unsigned char) value[i]));i--)
275 if ( ('"' == value[0]) &&
276 ('"' == value[strlen (value) - 1]) )
278 value[strlen (value) - 1] = '\0';
281 LOG (GNUNET_ERROR_TYPE_DEBUG, "Config value %s=\"%s\"\n", tag, value);
282 GNUNET_CONFIGURATION_set_value_string (cfg, section, tag, &value[i]);
287 LOG (GNUNET_ERROR_TYPE_WARNING,
288 _("Syntax error while deserializing in line %u\n"),
293 LOG (GNUNET_ERROR_TYPE_DEBUG, "Finished deserializing config\n");
294 GNUNET_free_non_null (line_orig);
295 GNUNET_free (section);
296 GNUNET_assert ( (GNUNET_OK != ret) || (r_bytes == size) );
302 * Parse a configuration file, add all of the options in the
303 * file to the configuration environment.
305 * @param cfg configuration to update
306 * @param filename name of the configuration file
307 * @return GNUNET_OK on success, GNUNET_SYSERR on error
310 GNUNET_CONFIGURATION_parse (struct GNUNET_CONFIGURATION_Handle *cfg,
311 const char *filename)
320 LOG (GNUNET_ERROR_TYPE_DEBUG, "Asked to parse config file `%s'\n", filename);
321 fn = GNUNET_STRINGS_filename_expand (filename);
322 LOG (GNUNET_ERROR_TYPE_DEBUG, "Config file name expanded to `%s'\n", fn);
324 return GNUNET_SYSERR;
325 dirty = cfg->dirty; /* back up value! */
327 GNUNET_DISK_file_size (fn, &fs64, GNUNET_YES, GNUNET_YES))
329 LOG (GNUNET_ERROR_TYPE_WARNING,
330 "Error while determining the file size of %s\n", fn);
332 return GNUNET_SYSERR;
336 GNUNET_break (0); /* File size is more than the heap size */
338 return GNUNET_SYSERR;
341 mem = GNUNET_malloc (fs);
342 if (fs != GNUNET_DISK_fn_read (fn, mem, fs))
344 LOG (GNUNET_ERROR_TYPE_WARNING,
345 "Error while reading file %s\n", fn);
348 return GNUNET_SYSERR;
350 LOG (GNUNET_ERROR_TYPE_DEBUG, "Deserializing contents of file `%s'\n", fn);
352 ret = GNUNET_CONFIGURATION_deserialize (cfg, mem, fs, GNUNET_YES);
354 /* restore dirty flag - anything we set in the meantime
362 * Test if there are configuration options that were
363 * changed since the last save.
365 * @param cfg configuration to inspect
366 * @return GNUNET_NO if clean, GNUNET_YES if dirty, GNUNET_SYSERR on error (i.e. last save failed)
369 GNUNET_CONFIGURATION_is_dirty (const struct GNUNET_CONFIGURATION_Handle *cfg)
376 * Serializes the given configuration.
378 * @param cfg configuration to serialize
379 * @param size will be set to the size of the serialized memory block
380 * @return the memory block where the serialized configuration is
381 * present. This memory should be freed by the caller
384 GNUNET_CONFIGURATION_serialize (const struct GNUNET_CONFIGURATION_Handle *cfg,
387 struct ConfigSection *sec;
388 struct ConfigEntry *ent;
398 /* Pass1 : calculate the buffer size required */
400 for (sec = cfg->sections; NULL != sec; sec = sec->next)
402 /* For each section we need to add 3 charaters: {'[',']','\n'} */
403 m_size += strlen (sec->name) + 3;
404 for (ent = sec->entries; NULL != ent; ent = ent->next)
406 if (NULL != ent->val)
408 /* if val has any '\n' then they occupy +1 character as '\n'->'\\','n' */
410 while (NULL != (pos = strstr (pos, "\n")))
415 /* For each key = value pair we need to add 4 characters (2
416 spaces and 1 equal-to character and 1 new line) */
417 m_size += strlen (ent->key) + strlen (ent->val) + 4;
420 /* A new line after section end */
424 /* Pass2: Allocate memory and write the configuration to it */
425 mem = GNUNET_malloc (m_size);
431 len = GNUNET_asprintf (&cbuf, "[%s]\n", sec->name);
432 GNUNET_assert (0 < len);
433 memcpy (mem + c_size, cbuf, len);
436 for (ent = sec->entries; NULL != ent; ent = ent->next)
438 if (NULL != ent->val)
440 val = GNUNET_malloc (strlen (ent->val) * 2 + 1);
441 strcpy (val, ent->val);
442 while (NULL != (pos = strstr (val, "\n")))
444 memmove (&pos[2], &pos[1], strlen (&pos[1]));
448 len = GNUNET_asprintf (&cbuf, "%s = %s\n", ent->key, val);
450 memcpy (mem + c_size, cbuf, len);
455 memcpy (mem + c_size, "\n", 1);
459 GNUNET_assert (c_size == m_size);
466 * Write configuration file.
468 * @param cfg configuration to write
469 * @param filename where to write the configuration
470 * @return GNUNET_OK on success, GNUNET_SYSERR on error
473 GNUNET_CONFIGURATION_write (struct GNUNET_CONFIGURATION_Handle *cfg,
474 const char *filename)
480 fn = GNUNET_STRINGS_filename_expand (filename);
482 return GNUNET_SYSERR;
483 if (GNUNET_OK != GNUNET_DISK_directory_create_for_file (fn))
486 return GNUNET_SYSERR;
488 cfg_buf = GNUNET_CONFIGURATION_serialize (cfg, &size);
489 if (size != GNUNET_DISK_fn_write (fn, cfg_buf, size,
490 GNUNET_DISK_PERM_USER_READ
491 | GNUNET_DISK_PERM_USER_WRITE
492 | GNUNET_DISK_PERM_GROUP_READ
493 | GNUNET_DISK_PERM_GROUP_WRITE))
496 GNUNET_free (cfg_buf);
497 LOG (GNUNET_ERROR_TYPE_WARNING,
498 "Writing configration to file: %s failed\n", filename);
499 cfg->dirty = GNUNET_SYSERR; /* last write failed */
500 return GNUNET_SYSERR;
503 GNUNET_free (cfg_buf);
504 cfg->dirty = GNUNET_NO; /* last write succeeded */
510 * Iterate over all options in the configuration.
512 * @param cfg configuration to inspect
513 * @param iter function to call on each option
514 * @param iter_cls closure for iter
517 GNUNET_CONFIGURATION_iterate (const struct GNUNET_CONFIGURATION_Handle *cfg,
518 GNUNET_CONFIGURATION_Iterator iter,
521 struct ConfigSection *spos;
522 struct ConfigEntry *epos;
524 for (spos = cfg->sections; NULL != spos; spos = spos->next)
525 for (epos = spos->entries; NULL != epos; epos = epos->next)
526 if (NULL != epos->val)
527 iter (iter_cls, spos->name, epos->key, epos->val);
532 * Iterate over values of a section in the configuration.
534 * @param cfg configuration to inspect
535 * @param section the section
536 * @param iter function to call on each option
537 * @param iter_cls closure for iter
540 GNUNET_CONFIGURATION_iterate_section_values (const struct
541 GNUNET_CONFIGURATION_Handle *cfg,
543 GNUNET_CONFIGURATION_Iterator iter,
546 struct ConfigSection *spos;
547 struct ConfigEntry *epos;
549 spos = cfg->sections;
550 while ((spos != NULL) && (0 != strcasecmp (spos->name, section)))
554 for (epos = spos->entries; NULL != epos; epos = epos->next)
555 if (NULL != epos->val)
556 iter (iter_cls, spos->name, epos->key, epos->val);
561 * Iterate over all sections in the configuration.
563 * @param cfg configuration to inspect
564 * @param iter function to call on each section
565 * @param iter_cls closure for iter
568 GNUNET_CONFIGURATION_iterate_sections (const struct GNUNET_CONFIGURATION_Handle
570 GNUNET_CONFIGURATION_Section_Iterator
571 iter, void *iter_cls)
573 struct ConfigSection *spos;
574 struct ConfigSection *next;
576 next = cfg->sections;
581 iter (iter_cls, spos->name);
586 * Remove the given section and all options in it.
588 * @param cfg configuration to inspect
589 * @param section name of the section to remove
592 GNUNET_CONFIGURATION_remove_section (struct GNUNET_CONFIGURATION_Handle *cfg,
595 struct ConfigSection *spos;
596 struct ConfigSection *prev;
597 struct ConfigEntry *ent;
600 spos = cfg->sections;
603 if (0 == strcasecmp (section, spos->name))
606 cfg->sections = spos->next;
608 prev->next = spos->next;
609 while (NULL != (ent = spos->entries))
611 spos->entries = ent->next;
612 GNUNET_free (ent->key);
613 GNUNET_free_non_null (ent->val);
615 cfg->dirty = GNUNET_YES;
617 GNUNET_free (spos->name);
628 * Copy a configuration value to the given target configuration.
629 * Overwrites existing entries.
631 * @param cls the destination configuration (struct GNUNET_CONFIGURATION_Handle*)
632 * @param section section for the value
633 * @param option option name of the value
634 * @param value value to copy
637 copy_entry (void *cls, const char *section, const char *option,
640 struct GNUNET_CONFIGURATION_Handle *dst = cls;
642 GNUNET_CONFIGURATION_set_value_string (dst, section, option, value);
647 * Duplicate an existing configuration object.
649 * @param cfg configuration to duplicate
650 * @return duplicate configuration
652 struct GNUNET_CONFIGURATION_Handle *
653 GNUNET_CONFIGURATION_dup (const struct GNUNET_CONFIGURATION_Handle *cfg)
655 struct GNUNET_CONFIGURATION_Handle *ret;
657 ret = GNUNET_CONFIGURATION_create ();
658 GNUNET_CONFIGURATION_iterate (cfg, ©_entry, ret);
664 * Find a section entry from a configuration.
667 * @param section FIXME
668 * @return matching entry, NULL if not found
670 static struct ConfigSection *
671 findSection (const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section)
673 struct ConfigSection *pos;
676 while ((pos != NULL) && (0 != strcasecmp (section, pos->name)))
683 * Find an entry from a configuration.
685 * @param cfg handle to the configuration
686 * @param section section the option is in
687 * @param key the option
688 * @return matching entry, NULL if not found
690 static struct ConfigEntry *
691 findEntry (const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section,
694 struct ConfigSection *sec;
695 struct ConfigEntry *pos;
697 if (NULL == (sec = findSection (cfg, section)))
700 while ((pos != NULL) && (0 != strcasecmp (key, pos->key)))
707 * A callback function, compares entries from two configurations
708 * (default against a new configuration) and write the diffs in a
709 * diff-configuration object (the callback object).
711 * @param cls the diff configuration (struct DiffHandle*)
712 * @param section section for the value (of the default conf.)
713 * @param option option name of the value (of the default conf.)
714 * @param value value to copy (of the default conf.)
717 compare_entries (void *cls, const char *section, const char *option,
720 struct DiffHandle *dh = cls;
721 struct ConfigEntry *entNew;
723 entNew = findEntry (dh->cfgDefault, section, option);
724 if ( (NULL != entNew) &&
725 (NULL != entNew->val) &&
726 (0 == strcmp (entNew->val, value)) )
728 GNUNET_CONFIGURATION_set_value_string (dh->cfgDiff, section, option, value);
733 * Compute configuration with only entries that have been changed
735 * @param cfgDefault original configuration
736 * @param cfgNew new configuration
737 * @return configuration with only the differences, never NULL
739 struct GNUNET_CONFIGURATION_Handle *
740 GNUNET_CONFIGURATION_get_diff (const struct GNUNET_CONFIGURATION_Handle
742 const struct GNUNET_CONFIGURATION_Handle
745 struct DiffHandle diffHandle;
747 diffHandle.cfgDiff = GNUNET_CONFIGURATION_create ();
748 diffHandle.cfgDefault = cfgDefault;
749 GNUNET_CONFIGURATION_iterate (cfgNew, &compare_entries, &diffHandle);
750 return diffHandle.cfgDiff;
755 * Write only configuration entries that have been changed to configuration file
756 * @param cfgDefault default configuration
757 * @param cfgNew new configuration
758 * @param filename where to write the configuration diff between default and new
759 * @return GNUNET_OK on success, GNUNET_SYSERR on error
762 GNUNET_CONFIGURATION_write_diffs (const struct GNUNET_CONFIGURATION_Handle
764 const struct GNUNET_CONFIGURATION_Handle
765 *cfgNew, const char *filename)
768 struct GNUNET_CONFIGURATION_Handle *diff;
770 diff = GNUNET_CONFIGURATION_get_diff (cfgDefault, cfgNew);
771 ret = GNUNET_CONFIGURATION_write (diff, filename);
772 GNUNET_CONFIGURATION_destroy (diff);
778 * Set a configuration value that should be a string.
780 * @param cfg configuration to update
781 * @param section section of interest
782 * @param option option of interest
783 * @param value value to set
786 GNUNET_CONFIGURATION_set_value_string (struct GNUNET_CONFIGURATION_Handle *cfg,
787 const char *section, const char *option,
790 struct ConfigSection *sec;
791 struct ConfigEntry *e;
794 e = findEntry (cfg, section, option);
799 GNUNET_free_non_null (e->val);
804 nv = GNUNET_strdup (value);
805 GNUNET_free_non_null (e->val);
810 sec = findSection (cfg, section);
813 sec = GNUNET_new (struct ConfigSection);
814 sec->name = GNUNET_strdup (section);
815 sec->next = cfg->sections;
818 e = GNUNET_new (struct ConfigEntry);
819 e->key = GNUNET_strdup (option);
820 e->val = GNUNET_strdup (value);
821 e->next = sec->entries;
827 * Set a configuration value that should be a number.
829 * @param cfg configuration to update
830 * @param section section of interest
831 * @param option option of interest
832 * @param number value to set
835 GNUNET_CONFIGURATION_set_value_number (struct GNUNET_CONFIGURATION_Handle *cfg,
836 const char *section, const char *option,
837 unsigned long long number)
841 GNUNET_snprintf (s, 64, "%llu", number);
842 GNUNET_CONFIGURATION_set_value_string (cfg, section, option, s);
847 * Get a configuration value that should be a number.
849 * @param cfg configuration to inspect
850 * @param section section of interest
851 * @param option option of interest
852 * @param number where to store the numeric value of the option
853 * @return GNUNET_OK on success, GNUNET_SYSERR on error
856 GNUNET_CONFIGURATION_get_value_number (const struct GNUNET_CONFIGURATION_Handle
857 *cfg, const char *section,
859 unsigned long long *number)
861 struct ConfigEntry *e;
863 if (NULL == (e = findEntry (cfg, section, option)))
864 return GNUNET_SYSERR;
866 return GNUNET_SYSERR;
867 if (1 != SSCANF (e->val, "%llu", number))
868 return GNUNET_SYSERR;
874 * Get a configuration value that should be a relative time.
876 * @param cfg configuration to inspect
877 * @param section section of interest
878 * @param option option of interest
879 * @param time set to the time value stored in the configuration
880 * @return GNUNET_OK on success, GNUNET_SYSERR on error
883 GNUNET_CONFIGURATION_get_value_time (const struct GNUNET_CONFIGURATION_Handle
884 *cfg, const char *section,
886 struct GNUNET_TIME_Relative *time)
888 struct ConfigEntry *e;
890 if (NULL == (e = findEntry (cfg, section, option)))
891 return GNUNET_SYSERR;
893 return GNUNET_SYSERR;
894 return GNUNET_STRINGS_fancy_time_to_relative (e->val, time);
899 * Get a configuration value that should be a size in bytes.
901 * @param cfg configuration to inspect
902 * @param section section of interest
903 * @param option option of interest
904 * @param size set to the size in bytes as stored in the configuration
905 * @return GNUNET_OK on success, GNUNET_SYSERR on error
908 GNUNET_CONFIGURATION_get_value_size (const struct GNUNET_CONFIGURATION_Handle
909 *cfg, const char *section,
911 unsigned long long *size)
913 struct ConfigEntry *e;
915 if (NULL == (e = findEntry (cfg, section, option)))
916 return GNUNET_SYSERR;
918 return GNUNET_SYSERR;
919 return GNUNET_STRINGS_fancy_size_to_bytes (e->val, size);
924 * Get a configuration value that should be a string.
926 * @param cfg configuration to inspect
927 * @param section section of interest
928 * @param option option of interest
929 * @param value will be set to a freshly allocated configuration
930 * value, or NULL if option is not specified
931 * @return GNUNET_OK on success, GNUNET_SYSERR on error
934 GNUNET_CONFIGURATION_get_value_string (const struct GNUNET_CONFIGURATION_Handle
935 *cfg, const char *section,
936 const char *option, char **value)
938 struct ConfigEntry *e;
940 LOG (GNUNET_ERROR_TYPE_DEBUG, "Asked to retrieve string `%s' in section `%s'\n", option, section);
941 if ( (NULL == (e = findEntry (cfg, section, option))) ||
945 return GNUNET_SYSERR;
947 *value = GNUNET_strdup (e->val);
953 * Get a configuration value that should be in a set of
956 * @param cfg configuration to inspect
957 * @param section section of interest
958 * @param option option of interest
959 * @param choices NULL-terminated list of legal values
960 * @param value will be set to an entry in the legal list,
961 * or NULL if option is not specified and no default given
962 * @return GNUNET_OK on success, GNUNET_SYSERR on error
965 GNUNET_CONFIGURATION_get_value_choice (const struct GNUNET_CONFIGURATION_Handle
966 *cfg, const char *section,
967 const char *option, const char *const *choices,
970 struct ConfigEntry *e;
973 if (NULL == (e = findEntry (cfg, section, option)))
974 return GNUNET_SYSERR;
975 for (i = 0; NULL != choices[i]; i++)
976 if (0 == strcasecmp (choices[i], e->val))
978 if (NULL == choices[i])
980 LOG (GNUNET_ERROR_TYPE_ERROR,
981 _("Configuration value '%s' for '%s'"
982 " in section '%s' is not in set of legal choices\n"), e->val, option,
984 return GNUNET_SYSERR;
992 * Test if we have a value for a particular option
993 * @param cfg configuration to inspect
994 * @param section section of interest
995 * @param option option of interest
996 * @return #GNUNET_YES if so, #GNUNET_NO if not.
999 GNUNET_CONFIGURATION_have_value (const struct GNUNET_CONFIGURATION_Handle *cfg,
1000 const char *section, const char *option)
1002 struct ConfigEntry *e;
1004 if ((NULL == (e = findEntry (cfg, section, option))) || (NULL == e->val))
1011 * Expand an expression of the form "$FOO/BAR" to "DIRECTORY/BAR"
1012 * where either in the "PATHS" section or the environtment "FOO" is
1013 * set to "DIRECTORY". We also support default expansion,
1014 * i.e. ${VARIABLE:-default} will expand to $VARIABLE if VARIABLE is
1015 * set in PATHS or the environment, and otherwise to "default". Note
1016 * that "default" itself can also be a $-expression, thus
1017 * "${VAR1:-{$VAR2}}" will expand to VAR1 and if that is not defined
1020 * @param cfg configuration to use for path expansion
1021 * @param orig string to $-expand (will be freed!)
1022 * @param depth recursion depth, used to detect recursive expansions
1023 * @return $-expanded string
1026 expand_dollar (const struct GNUNET_CONFIGURATION_Handle *cfg,
1042 LOG (GNUNET_ERROR_TYPE_WARNING,
1043 _("Recursive expansion suspected, aborting $-expansion for term `%s'\n"),
1047 LOG (GNUNET_ERROR_TYPE_DEBUG,
1048 "Asked to $-expand %s\n", orig);
1051 LOG (GNUNET_ERROR_TYPE_DEBUG,
1052 "Doesn't start with $ - not expanding\n");
1072 LOG (GNUNET_ERROR_TYPE_WARNING,
1073 _("Missing closing `%s' in option `%s'\n"),
1083 def = strchr (orig, ':');
1088 if ( ('-' == *def) ||
1091 def = GNUNET_strdup (def);
1099 while ( (orig[i] != '/') &&
1100 (orig[i] != '\\') &&
1103 if (orig[i] == '\0')
1110 post = &orig[i + 1];
1113 LOG (GNUNET_ERROR_TYPE_DEBUG,
1114 "Split into `%s' and `%s' with default %s\n",
1119 GNUNET_CONFIGURATION_get_value_filename (cfg,
1124 LOG (GNUNET_ERROR_TYPE_DEBUG,
1125 "Filename for `%s' is not in PATHS config section\n",
1127 if (NULL == (env = getenv (start)))
1129 LOG (GNUNET_ERROR_TYPE_DEBUG,
1130 "`%s' is not an environment variable\n",
1133 def = expand_dollar (cfg, def, depth + 1);
1138 orig[strlen (orig)] = DIR_SEPARATOR;
1139 LOG (GNUNET_ERROR_TYPE_DEBUG,
1140 "Expanded to `%s' (returning orig)\n",
1144 prefix = GNUNET_strdup (env);
1146 LOG (GNUNET_ERROR_TYPE_DEBUG,
1149 result = GNUNET_malloc (strlen (prefix) + strlen (post) + 2);
1150 strcpy (result, prefix);
1151 if ( (0 == strlen (prefix)) ||
1152 ( (prefix[strlen (prefix) - 1] != DIR_SEPARATOR) &&
1153 (strlen (post) > 0) ) )
1154 strcat (result, DIR_SEPARATOR_STR);
1155 strcat (result, post);
1156 GNUNET_free_non_null (def);
1157 GNUNET_free (prefix);
1159 LOG (GNUNET_ERROR_TYPE_DEBUG,
1160 "Expanded to `%s'\n",
1167 * Expand an expression of the form "$FOO/BAR" to "DIRECTORY/BAR"
1168 * where either in the "PATHS" section or the environtment "FOO" is
1169 * set to "DIRECTORY". We also support default expansion,
1170 * i.e. ${VARIABLE:-default} will expand to $VARIABLE if VARIABLE is
1171 * set in PATHS or the environment, and otherwise to "default". Note
1172 * that "default" itself can also be a $-expression, thus
1173 * "${VAR1:-{$VAR2}}" will expand to VAR1 and if that is not defined
1176 * @param cfg configuration to use for path expansion
1177 * @param orig string to $-expand (will be freed!)
1178 * @return $-expanded string
1181 GNUNET_CONFIGURATION_expand_dollar (const struct GNUNET_CONFIGURATION_Handle *cfg,
1184 return expand_dollar (cfg, orig, 0);
1189 * Get a configuration value that should be a string.
1191 * @param cfg configuration to inspect
1192 * @param section section of interest
1193 * @param option option of interest
1194 * @param value will be set to a freshly allocated configuration
1195 * value, or NULL if option is not specified
1196 * @return GNUNET_OK on success, GNUNET_SYSERR on error
1199 GNUNET_CONFIGURATION_get_value_filename (const struct
1200 GNUNET_CONFIGURATION_Handle *cfg,
1201 const char *section,
1202 const char *option, char **value)
1206 LOG (GNUNET_ERROR_TYPE_DEBUG, "Asked to retrieve filename `%s' in section `%s'\n", option, section);
1208 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &tmp))
1210 LOG (GNUNET_ERROR_TYPE_DEBUG, "Failed to retrieve filename\n");
1212 return GNUNET_SYSERR;
1214 LOG (GNUNET_ERROR_TYPE_DEBUG, "Retrieved filename `%s', $-expanding\n", tmp);
1215 tmp = GNUNET_CONFIGURATION_expand_dollar (cfg, tmp);
1216 LOG (GNUNET_ERROR_TYPE_DEBUG, "Expanded to filename `%s', *nix-expanding\n", tmp);
1217 *value = GNUNET_STRINGS_filename_expand (tmp);
1219 LOG (GNUNET_ERROR_TYPE_DEBUG, "Filename result is `%s'\n", *value);
1221 return GNUNET_SYSERR;
1227 * Get a configuration value that should be in a set of
1228 * "GNUNET_YES" or "GNUNET_NO".
1230 * @param cfg configuration to inspect
1231 * @param section section of interest
1232 * @param option option of interest
1233 * @return GNUNET_YES, GNUNET_NO or GNUNET_SYSERR
1236 GNUNET_CONFIGURATION_get_value_yesno (const struct GNUNET_CONFIGURATION_Handle
1237 *cfg, const char *section,
1240 static const char *yesno[] = { "YES", "NO", NULL };
1245 GNUNET_CONFIGURATION_get_value_choice (cfg, section, option, yesno, &val);
1246 if (ret == GNUNET_SYSERR)
1248 if (val == yesno[0])
1255 * Iterate over the set of filenames stored in a configuration value.
1257 * @param cfg configuration to inspect
1258 * @param section section of interest
1259 * @param option option of interest
1260 * @param cb function to call on each filename
1261 * @param cb_cls closure for cb
1262 * @return number of filenames iterated over, -1 on error
1265 GNUNET_CONFIGURATION_iterate_value_filenames (const struct
1266 GNUNET_CONFIGURATION_Handle *cfg,
1267 const char *section,
1269 GNUNET_FileNameCallback cb,
1279 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &list))
1281 GNUNET_assert (list != NULL);
1286 while (pos[0] == ' ')
1288 if (strlen (pos) == 0)
1291 while ((end[0] != ' ') && (end[0] != '\0'))
1299 memmove (end, &end[1], strlen (&end[1]) + 1);
1301 /* illegal, but just keep it */
1304 /* illegal, but just ignore that there was a '/' */
1312 if (strlen (pos) > 0)
1315 if ((cb != NULL) && (GNUNET_OK != cb (cb_cls, pos)))
1317 ret = GNUNET_SYSERR;
1333 * @param value FIXME
1337 escape_name (const char *value)
1343 escaped = GNUNET_malloc (strlen (value) * 2 + 1);
1344 memset (escaped, 0, strlen (value) * 2 + 1);
1347 while (rpos[0] != '\0')
1370 * @param cls string we compare with (const char*)
1371 * @param fn filename we are currently looking at
1372 * @return GNUNET_OK if the names do not match, GNUNET_SYSERR if they do
1375 test_match (void *cls, const char *fn)
1377 const char *of = cls;
1379 return (0 == strcmp (of, fn)) ? GNUNET_SYSERR : GNUNET_OK;
1384 * Append a filename to a configuration value that
1385 * represents a list of filenames
1387 * @param cfg configuration to update
1388 * @param section section of interest
1389 * @param option option of interest
1390 * @param value filename to append
1391 * @return GNUNET_OK on success,
1392 * GNUNET_NO if the filename already in the list
1393 * GNUNET_SYSERR on error
1396 GNUNET_CONFIGURATION_append_value_filename (struct GNUNET_CONFIGURATION_Handle
1397 *cfg, const char *section,
1405 if (GNUNET_SYSERR ==
1406 GNUNET_CONFIGURATION_iterate_value_filenames (cfg, section, option,
1409 return GNUNET_NO; /* already exists */
1411 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &old))
1412 old = GNUNET_strdup ("");
1413 escaped = escape_name (value);
1414 nw = GNUNET_malloc (strlen (old) + strlen (escaped) + 2);
1416 if (strlen (old) > 0)
1418 strcat (nw, escaped);
1419 GNUNET_CONFIGURATION_set_value_string (cfg, section, option, nw);
1422 GNUNET_free (escaped);
1428 * Remove a filename from a configuration value that
1429 * represents a list of filenames
1431 * @param cfg configuration to update
1432 * @param section section of interest
1433 * @param option option of interest
1434 * @param value filename to remove
1435 * @return GNUNET_OK on success,
1436 * GNUNET_NO if the filename is not in the list,
1437 * GNUNET_SYSERR on error
1440 GNUNET_CONFIGURATION_remove_value_filename (struct GNUNET_CONFIGURATION_Handle
1441 *cfg, const char *section,
1452 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &list))
1454 match = escape_name (value);
1458 while (pos[0] == ' ')
1460 if (strlen (pos) == 0)
1463 while ((end[0] != ' ') && (end[0] != '\0'))
1474 /* illegal, but just keep it */
1477 /* illegal, but just ignore that there was a '/' */
1485 if (0 == strcmp (pos, match))
1488 memmove (pos, &end[1], strlen (&end[1]) + 1);
1496 GNUNET_CONFIGURATION_set_value_string (cfg, section, option, list);
1498 GNUNET_free (match);
1507 GNUNET_free (match);
1513 * Wrapper around GNUNET_CONFIGURATION_parse.
1515 * @param cls the cfg
1516 * @param filename file to parse
1517 * @return GNUNET_OK on success
1520 parse_configuration_file (void *cls, const char *filename)
1522 struct GNUNET_CONFIGURATION_Handle *cfg = cls;
1526 /* Examine file extension */
1527 ext = strrchr (filename, '.');
1528 if ((NULL == ext) || (0 != strcmp (ext, ".conf")))
1530 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Skipping file `%s'\n", filename);
1534 ret = GNUNET_CONFIGURATION_parse (cfg, filename);
1540 * Load default configuration. This function will parse the
1541 * defaults from the given defaults_d directory.
1543 * @param cfg configuration to update
1544 * @param defaults_d directory with the defaults
1545 * @return GNUNET_OK on success, GNUNET_SYSERR on error
1548 GNUNET_CONFIGURATION_load_from (struct GNUNET_CONFIGURATION_Handle *cfg,
1549 const char *defaults_d)
1551 if (GNUNET_SYSERR ==
1552 GNUNET_DISK_directory_scan (defaults_d, &parse_configuration_file, cfg))
1553 return GNUNET_SYSERR; /* no configuration at all found */
1559 * Load configuration (starts with defaults, then loads
1560 * system-specific configuration).
1562 * @param cfg configuration to update
1563 * @param filename name of the configuration file, NULL to load defaults
1564 * @return GNUNET_OK on success, GNUNET_SYSERR on error
1567 GNUNET_CONFIGURATION_load (struct GNUNET_CONFIGURATION_Handle *cfg,
1568 const char *filename)
1573 ipath = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_DATADIR);
1575 return GNUNET_SYSERR;
1577 GNUNET_asprintf (&baseconfig, "%s%s", ipath, "config.d");
1578 GNUNET_free (ipath);
1579 if (GNUNET_SYSERR ==
1580 GNUNET_DISK_directory_scan (baseconfig, &parse_configuration_file, cfg))
1582 GNUNET_free (baseconfig);
1583 return GNUNET_SYSERR; /* no configuration at all found */
1585 GNUNET_free (baseconfig);
1586 if ((filename != NULL) &&
1587 (GNUNET_OK != GNUNET_CONFIGURATION_parse (cfg, filename)))
1589 /* specified configuration not found */
1590 return GNUNET_SYSERR;
1593 GNUNET_CONFIGURATION_have_value (cfg, "PATHS", "DEFAULTCONFIG"))) &&
1595 GNUNET_CONFIGURATION_set_value_string (cfg, "PATHS", "DEFAULTCONFIG",
1602 /* end of configuration.c */