2 This file is part of GNUnet.
3 (C) 2006, 2007, 2008, 2009 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 **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
1013 * "FOO" is set to "DIRECTORY".
1015 * @param cfg configuration to use for path expansion
1016 * @param orig string to $-expand (will be freed!)
1017 * @return $-expanded string
1020 GNUNET_CONFIGURATION_expand_dollar (const struct GNUNET_CONFIGURATION_Handle
1029 LOG (GNUNET_ERROR_TYPE_DEBUG, "Asked to $-expand %s\n", orig);
1033 LOG (GNUNET_ERROR_TYPE_DEBUG, "Doesn't start with $ - not expanding\n");
1037 while ((orig[i] != '/') && (orig[i] != '\\') && (orig[i] != '\0'))
1039 if (orig[i] == '\0')
1046 post = &orig[i + 1];
1048 LOG (GNUNET_ERROR_TYPE_DEBUG, "Split into `%s' and `%s'\n", orig, post);
1050 GNUNET_CONFIGURATION_get_value_filename (cfg, "PATHS", &orig[1], &prefix))
1052 LOG (GNUNET_ERROR_TYPE_DEBUG, "Filename for `%s' is not in PATHS config section\n", &orig[1]);
1053 if (NULL == (env = getenv (&orig[1])))
1055 LOG (GNUNET_ERROR_TYPE_DEBUG, "`%s' is not an environment variable\n", &orig[1]);
1056 orig[i] = DIR_SEPARATOR;
1057 LOG (GNUNET_ERROR_TYPE_DEBUG, "Expanded to `%s' (returning orig)\n", orig);
1060 prefix = GNUNET_strdup (env);
1062 LOG (GNUNET_ERROR_TYPE_DEBUG, "Prefix is `%s'\n", prefix);
1063 result = GNUNET_malloc (strlen (prefix) + strlen (post) + 2);
1064 strcpy (result, prefix);
1065 if ((strlen (prefix) == 0) ||
1066 ((prefix[strlen (prefix) - 1] != DIR_SEPARATOR) && (strlen (post) > 0)))
1067 strcat (result, DIR_SEPARATOR_STR);
1068 strcat (result, post);
1069 GNUNET_free (prefix);
1071 LOG (GNUNET_ERROR_TYPE_DEBUG, "Expanded to `%s'\n", result);
1077 * Get a configuration value that should be a string.
1079 * @param cfg configuration to inspect
1080 * @param section section of interest
1081 * @param option option of interest
1082 * @param value will be set to a freshly allocated configuration
1083 * value, or NULL if option is not specified
1084 * @return GNUNET_OK on success, GNUNET_SYSERR on error
1087 GNUNET_CONFIGURATION_get_value_filename (const struct
1088 GNUNET_CONFIGURATION_Handle *cfg,
1089 const char *section,
1090 const char *option, char **value)
1094 LOG (GNUNET_ERROR_TYPE_DEBUG, "Asked to retrieve filename `%s' in section `%s'\n", option, section);
1096 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &tmp))
1098 LOG (GNUNET_ERROR_TYPE_DEBUG, "Failed to retrieve filename\n");
1100 return GNUNET_SYSERR;
1102 LOG (GNUNET_ERROR_TYPE_DEBUG, "Retrieved filename `%s', $-expanding\n", tmp);
1103 tmp = GNUNET_CONFIGURATION_expand_dollar (cfg, tmp);
1104 LOG (GNUNET_ERROR_TYPE_DEBUG, "Expanded to filename `%s', *nix-expanding\n", tmp);
1105 *value = GNUNET_STRINGS_filename_expand (tmp);
1107 LOG (GNUNET_ERROR_TYPE_DEBUG, "Filename result is `%s'\n", *value);
1109 return GNUNET_SYSERR;
1115 * Get a configuration value that should be in a set of
1116 * "GNUNET_YES" or "GNUNET_NO".
1118 * @param cfg configuration to inspect
1119 * @param section section of interest
1120 * @param option option of interest
1121 * @return GNUNET_YES, GNUNET_NO or GNUNET_SYSERR
1124 GNUNET_CONFIGURATION_get_value_yesno (const struct GNUNET_CONFIGURATION_Handle
1125 *cfg, const char *section,
1128 static const char *yesno[] = { "YES", "NO", NULL };
1133 GNUNET_CONFIGURATION_get_value_choice (cfg, section, option, yesno, &val);
1134 if (ret == GNUNET_SYSERR)
1136 if (val == yesno[0])
1143 * Iterate over the set of filenames stored in a configuration value.
1145 * @param cfg configuration to inspect
1146 * @param section section of interest
1147 * @param option option of interest
1148 * @param cb function to call on each filename
1149 * @param cb_cls closure for cb
1150 * @return number of filenames iterated over, -1 on error
1153 GNUNET_CONFIGURATION_iterate_value_filenames (const struct
1154 GNUNET_CONFIGURATION_Handle *cfg,
1155 const char *section,
1157 GNUNET_FileNameCallback cb,
1167 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &list))
1169 GNUNET_assert (list != NULL);
1174 while (pos[0] == ' ')
1176 if (strlen (pos) == 0)
1179 while ((end[0] != ' ') && (end[0] != '\0'))
1187 memmove (end, &end[1], strlen (&end[1]) + 1);
1189 /* illegal, but just keep it */
1192 /* illegal, but just ignore that there was a '/' */
1200 if (strlen (pos) > 0)
1203 if ((cb != NULL) && (GNUNET_OK != cb (cb_cls, pos)))
1205 ret = GNUNET_SYSERR;
1221 * @param value FIXME
1225 escape_name (const char *value)
1231 escaped = GNUNET_malloc (strlen (value) * 2 + 1);
1232 memset (escaped, 0, strlen (value) * 2 + 1);
1235 while (rpos[0] != '\0')
1258 * @param cls string we compare with (const char*)
1259 * @param fn filename we are currently looking at
1260 * @return GNUNET_OK if the names do not match, GNUNET_SYSERR if they do
1263 test_match (void *cls, const char *fn)
1265 const char *of = cls;
1267 return (0 == strcmp (of, fn)) ? GNUNET_SYSERR : GNUNET_OK;
1272 * Append a filename to a configuration value that
1273 * represents a list of filenames
1275 * @param cfg configuration to update
1276 * @param section section of interest
1277 * @param option option of interest
1278 * @param value filename to append
1279 * @return GNUNET_OK on success,
1280 * GNUNET_NO if the filename already in the list
1281 * GNUNET_SYSERR on error
1284 GNUNET_CONFIGURATION_append_value_filename (struct GNUNET_CONFIGURATION_Handle
1285 *cfg, const char *section,
1293 if (GNUNET_SYSERR ==
1294 GNUNET_CONFIGURATION_iterate_value_filenames (cfg, section, option,
1297 return GNUNET_NO; /* already exists */
1299 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &old))
1300 old = GNUNET_strdup ("");
1301 escaped = escape_name (value);
1302 nw = GNUNET_malloc (strlen (old) + strlen (escaped) + 2);
1304 if (strlen (old) > 0)
1306 strcat (nw, escaped);
1307 GNUNET_CONFIGURATION_set_value_string (cfg, section, option, nw);
1310 GNUNET_free (escaped);
1316 * Remove a filename from a configuration value that
1317 * represents a list of filenames
1319 * @param cfg configuration to update
1320 * @param section section of interest
1321 * @param option option of interest
1322 * @param value filename to remove
1323 * @return GNUNET_OK on success,
1324 * GNUNET_NO if the filename is not in the list,
1325 * GNUNET_SYSERR on error
1328 GNUNET_CONFIGURATION_remove_value_filename (struct GNUNET_CONFIGURATION_Handle
1329 *cfg, const char *section,
1340 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &list))
1342 match = escape_name (value);
1346 while (pos[0] == ' ')
1348 if (strlen (pos) == 0)
1351 while ((end[0] != ' ') && (end[0] != '\0'))
1362 /* illegal, but just keep it */
1365 /* illegal, but just ignore that there was a '/' */
1373 if (0 == strcmp (pos, match))
1376 memmove (pos, &end[1], strlen (&end[1]) + 1);
1384 GNUNET_CONFIGURATION_set_value_string (cfg, section, option, list);
1386 GNUNET_free (match);
1395 GNUNET_free (match);
1401 * Wrapper around GNUNET_CONFIGURATION_parse.
1403 * @param cls the cfg
1404 * @param filename file to parse
1405 * @return GNUNET_OK on success
1408 parse_configuration_file (void *cls, const char *filename)
1410 struct GNUNET_CONFIGURATION_Handle *cfg = cls;
1414 /* Examine file extension */
1415 ext = strrchr (filename, '.');
1416 if ((NULL == ext) || (0 != strcmp (ext, ".conf")))
1418 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Skipping file `%s'\n", filename);
1422 ret = GNUNET_CONFIGURATION_parse (cfg, filename);
1428 * Load default configuration. This function will parse the
1429 * defaults from the given defaults_d directory.
1431 * @param cfg configuration to update
1432 * @param defaults_d directory with the defaults
1433 * @return GNUNET_OK on success, GNUNET_SYSERR on error
1436 GNUNET_CONFIGURATION_load_from (struct GNUNET_CONFIGURATION_Handle *cfg,
1437 const char *defaults_d)
1439 if (GNUNET_SYSERR ==
1440 GNUNET_DISK_directory_scan (defaults_d, &parse_configuration_file, cfg))
1441 return GNUNET_SYSERR; /* no configuration at all found */
1447 * Load configuration (starts with defaults, then loads
1448 * system-specific configuration).
1450 * @param cfg configuration to update
1451 * @param filename name of the configuration file, NULL to load defaults
1452 * @return GNUNET_OK on success, GNUNET_SYSERR on error
1455 GNUNET_CONFIGURATION_load (struct GNUNET_CONFIGURATION_Handle *cfg,
1456 const char *filename)
1461 ipath = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_DATADIR);
1463 return GNUNET_SYSERR;
1465 GNUNET_asprintf (&baseconfig, "%s%s", ipath, "config.d");
1466 GNUNET_free (ipath);
1467 if (GNUNET_SYSERR ==
1468 GNUNET_DISK_directory_scan (baseconfig, &parse_configuration_file, cfg))
1470 GNUNET_free (baseconfig);
1471 return GNUNET_SYSERR; /* no configuration at all found */
1473 GNUNET_free (baseconfig);
1474 if ((filename != NULL) &&
1475 (GNUNET_OK != GNUNET_CONFIGURATION_parse (cfg, filename)))
1477 /* specified configuration not found */
1478 return GNUNET_SYSERR;
1481 GNUNET_CONFIGURATION_have_value (cfg, "PATHS", "DEFAULTCONFIG"))) &&
1483 GNUNET_CONFIGURATION_set_value_string (cfg, "PATHS", "DEFAULTCONFIG",
1490 /* end of configuration.c */