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 2, 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_common.h"
29 #include "gnunet_util_lib.h"
30 #include "gnunet_crypto_lib.h"
31 #include "gnunet_strings_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 *cfgDefault;
109 struct GNUNET_CONFIGURATION_Handle *cfgDiff;
115 * Create a GNUNET_CONFIGURATION_Handle.
117 * @return fresh configuration object
119 struct GNUNET_CONFIGURATION_Handle *
120 GNUNET_CONFIGURATION_create ()
122 return GNUNET_new (struct GNUNET_CONFIGURATION_Handle);
127 * Destroy configuration object.
129 * @param cfg configuration to destroy
132 GNUNET_CONFIGURATION_destroy (struct GNUNET_CONFIGURATION_Handle *cfg)
134 struct ConfigSection *sec;
136 while (NULL != (sec = cfg->sections))
137 GNUNET_CONFIGURATION_remove_section (cfg, sec->name);
143 * De-serializes configuration
145 * @param cfg configuration to update
146 * @param mem the memory block of serialized configuration
147 * @param size the size of the memory block
148 * @param allow_inline set to GNUNET_YES if we recursively load configuration
149 * from inlined configurations; GNUNET_NO if not and raise warnings
150 * when we come across them
151 * @return GNUNET_OK on success, GNUNET_ERROR on error
154 GNUNET_CONFIGURATION_deserialize (struct GNUNET_CONFIGURATION_Handle *cfg,
174 LOG (GNUNET_ERROR_TYPE_DEBUG, "Deserializing config file\n");
176 section = GNUNET_strdup ("");
180 while (r_bytes < size)
182 GNUNET_free_non_null (line_orig);
183 /* fgets-like behaviour on buffer */
184 to_read = size - r_bytes;
185 pos = memchr (&mem[r_bytes], '\n', to_read);
188 line_orig = GNUNET_strndup (&mem[r_bytes], line_size = to_read);
189 r_bytes += line_size;
193 line_orig = GNUNET_strndup (&mem[r_bytes], line_size = (pos - &mem[r_bytes]));
194 r_bytes += line_size + 1;
197 /* increment line number */
199 /* tabs and '\r' are whitespace */
200 emptyline = GNUNET_YES;
201 for (i = 0; i < line_size; i++)
208 emptyline = GNUNET_NO;
210 /* ignore empty lines */
211 if (GNUNET_YES == emptyline)
214 /* remove tailing whitespace */
215 for (i = line_size - 1; (i >= 1) && (isspace ((unsigned char) line[i]));i--)
218 /* remove leading whitespace */
219 for (; line[0] != '\0' && (isspace ((unsigned char) line[0])); line++);
221 /* ignore comments */
222 if ( ('#' == line[0]) || ('%' == line[0]) )
225 /* handle special "@INLINE@" directive */
226 if (0 == strncasecmp (line,
228 strlen ("@INLINE@ ")))
231 value = &line[strlen ("@INLINE@ ")];
232 if (GNUNET_YES == allow_inline)
234 if (GNUNET_OK != GNUNET_CONFIGURATION_parse (cfg, value))
236 ret = GNUNET_SYSERR; /* failed to parse included config */
242 LOG (GNUNET_ERROR_TYPE_DEBUG,
243 "Ignoring parsing @INLINE@ configurations, not allowed!\n");
249 if ( ('[' == line[0]) && (']' == line[line_size - 1]) )
252 line[line_size - 1] = '\0';
254 GNUNET_free (section);
255 section = GNUNET_strdup (value);
256 LOG (GNUNET_ERROR_TYPE_DEBUG,
257 "Config section `%s'\n",
261 if (NULL != (eq = strchr (line, '=')))
264 tag = GNUNET_strndup (line, eq - line);
265 /* remove tailing whitespace */
266 for (i = strlen (tag) - 1; (i >= 1) && (isspace ((unsigned char) tag[i]));i--)
269 /* Strip whitespace */
271 while (isspace ((unsigned char) value[0]))
273 for (i = strlen (value) - 1; (i >= 1) && (isspace ((unsigned char) value[i]));i--)
278 if ( ('"' == value[0]) &&
279 ('"' == value[strlen (value) - 1]) )
281 value[strlen (value) - 1] = '\0';
284 LOG (GNUNET_ERROR_TYPE_DEBUG, "Config value %s=\"%s\"\n", tag, value);
285 GNUNET_CONFIGURATION_set_value_string (cfg, section, tag, &value[i]);
290 LOG (GNUNET_ERROR_TYPE_WARNING,
291 _("Syntax error while deserializing in line %u\n"),
296 LOG (GNUNET_ERROR_TYPE_DEBUG, "Finished deserializing config\n");
297 GNUNET_free_non_null (line_orig);
298 GNUNET_free (section);
299 GNUNET_assert ( (GNUNET_OK != ret) || (r_bytes == size) );
305 * Parse a configuration file, add all of the options in the
306 * file to the configuration environment.
308 * @param cfg configuration to update
309 * @param filename name of the configuration file
310 * @return GNUNET_OK on success, GNUNET_SYSERR on error
313 GNUNET_CONFIGURATION_parse (struct GNUNET_CONFIGURATION_Handle *cfg,
314 const char *filename)
323 LOG (GNUNET_ERROR_TYPE_DEBUG, "Asked to parse config file `%s'\n", filename);
324 fn = GNUNET_STRINGS_filename_expand (filename);
325 LOG (GNUNET_ERROR_TYPE_DEBUG, "Config file name expanded to `%s'\n", fn);
327 return GNUNET_SYSERR;
328 dirty = cfg->dirty; /* back up value! */
330 GNUNET_DISK_file_size (fn, &fs64, GNUNET_YES, GNUNET_YES))
332 LOG (GNUNET_ERROR_TYPE_WARNING,
333 "Error while determining the file size of %s\n", fn);
335 return GNUNET_SYSERR;
339 GNUNET_break (0); /* File size is more than the heap size */
341 return GNUNET_SYSERR;
344 mem = GNUNET_malloc (fs);
345 if (fs != GNUNET_DISK_fn_read (fn, mem, fs))
347 LOG (GNUNET_ERROR_TYPE_WARNING,
348 "Error while reading file %s\n", fn);
351 return GNUNET_SYSERR;
353 LOG (GNUNET_ERROR_TYPE_DEBUG, "Deserializing contents of file `%s'\n", fn);
355 ret = GNUNET_CONFIGURATION_deserialize (cfg, mem, fs, GNUNET_YES);
357 /* restore dirty flag - anything we set in the meantime
365 * Test if there are configuration options that were
366 * changed since the last save.
368 * @param cfg configuration to inspect
369 * @return GNUNET_NO if clean, GNUNET_YES if dirty, GNUNET_SYSERR on error (i.e. last save failed)
372 GNUNET_CONFIGURATION_is_dirty (const struct GNUNET_CONFIGURATION_Handle *cfg)
379 * Serializes the given configuration.
381 * @param cfg configuration to serialize
382 * @param size will be set to the size of the serialized memory block
383 * @return the memory block where the serialized configuration is
384 * present. This memory should be freed by the caller
387 GNUNET_CONFIGURATION_serialize (const struct GNUNET_CONFIGURATION_Handle *cfg,
390 struct ConfigSection *sec;
391 struct ConfigEntry *ent;
401 /* Pass1 : calculate the buffer size required */
403 for (sec = cfg->sections; NULL != sec; sec = sec->next)
405 /* For each section we need to add 3 charaters: {'[',']','\n'} */
406 m_size += strlen (sec->name) + 3;
407 for (ent = sec->entries; NULL != ent; ent = ent->next)
409 if (NULL != ent->val)
411 /* if val has any '\n' then they occupy +1 character as '\n'->'\\','n' */
413 while (NULL != (pos = strstr (pos, "\n")))
418 /* For each key = value pair we need to add 4 characters (2
419 spaces and 1 equal-to character and 1 new line) */
420 m_size += strlen (ent->key) + strlen (ent->val) + 4;
423 /* A new line after section end */
427 /* Pass2: Allocate memory and write the configuration to it */
428 mem = GNUNET_malloc (m_size);
434 len = GNUNET_asprintf (&cbuf, "[%s]\n", sec->name);
435 GNUNET_assert (0 < len);
436 memcpy (mem + c_size, cbuf, len);
439 for (ent = sec->entries; NULL != ent; ent = ent->next)
441 if (NULL != ent->val)
443 val = GNUNET_malloc (strlen (ent->val) * 2 + 1);
444 strcpy (val, ent->val);
445 while (NULL != (pos = strstr (val, "\n")))
447 memmove (&pos[2], &pos[1], strlen (&pos[1]));
451 len = GNUNET_asprintf (&cbuf, "%s = %s\n", ent->key, val);
453 memcpy (mem + c_size, cbuf, len);
458 memcpy (mem + c_size, "\n", 1);
462 GNUNET_assert (c_size == m_size);
469 * Write configuration file.
471 * @param cfg configuration to write
472 * @param filename where to write the configuration
473 * @return GNUNET_OK on success, GNUNET_SYSERR on error
476 GNUNET_CONFIGURATION_write (struct GNUNET_CONFIGURATION_Handle *cfg,
477 const char *filename)
483 fn = GNUNET_STRINGS_filename_expand (filename);
485 return GNUNET_SYSERR;
486 if (GNUNET_OK != GNUNET_DISK_directory_create_for_file (fn))
489 return GNUNET_SYSERR;
491 cfg_buf = GNUNET_CONFIGURATION_serialize (cfg, &size);
492 if (size != GNUNET_DISK_fn_write (fn, cfg_buf, size,
493 GNUNET_DISK_PERM_USER_READ
494 | GNUNET_DISK_PERM_USER_WRITE
495 | GNUNET_DISK_PERM_GROUP_READ
496 | GNUNET_DISK_PERM_GROUP_WRITE))
499 GNUNET_free (cfg_buf);
500 LOG (GNUNET_ERROR_TYPE_WARNING,
501 "Writing configration to file: %s failed\n", filename);
502 cfg->dirty = GNUNET_SYSERR; /* last write failed */
503 return GNUNET_SYSERR;
506 GNUNET_free (cfg_buf);
507 cfg->dirty = GNUNET_NO; /* last write succeeded */
513 * Iterate over all options in the configuration.
515 * @param cfg configuration to inspect
516 * @param iter function to call on each option
517 * @param iter_cls closure for iter
520 GNUNET_CONFIGURATION_iterate (const struct GNUNET_CONFIGURATION_Handle *cfg,
521 GNUNET_CONFIGURATION_Iterator iter,
524 struct ConfigSection *spos;
525 struct ConfigEntry *epos;
527 for (spos = cfg->sections; NULL != spos; spos = spos->next)
528 for (epos = spos->entries; NULL != epos; epos = epos->next)
529 if (NULL != epos->val)
530 iter (iter_cls, spos->name, epos->key, epos->val);
535 * Iterate over values of a section in the configuration.
537 * @param cfg configuration to inspect
538 * @param section the section
539 * @param iter function to call on each option
540 * @param iter_cls closure for iter
543 GNUNET_CONFIGURATION_iterate_section_values (const struct
544 GNUNET_CONFIGURATION_Handle *cfg,
546 GNUNET_CONFIGURATION_Iterator iter,
549 struct ConfigSection *spos;
550 struct ConfigEntry *epos;
552 spos = cfg->sections;
553 while ((spos != NULL) && (0 != strcasecmp (spos->name, section)))
557 for (epos = spos->entries; NULL != epos; epos = epos->next)
558 if (NULL != epos->val)
559 iter (iter_cls, spos->name, epos->key, epos->val);
564 * Iterate over all sections in the configuration.
566 * @param cfg configuration to inspect
567 * @param iter function to call on each section
568 * @param iter_cls closure for iter
571 GNUNET_CONFIGURATION_iterate_sections (const struct GNUNET_CONFIGURATION_Handle
573 GNUNET_CONFIGURATION_Section_Iterator
574 iter, void *iter_cls)
576 struct ConfigSection *spos;
577 struct ConfigSection *next;
579 next = cfg->sections;
584 iter (iter_cls, spos->name);
589 * Remove the given section and all options in it.
591 * @param cfg configuration to inspect
592 * @param section name of the section to remove
595 GNUNET_CONFIGURATION_remove_section (struct GNUNET_CONFIGURATION_Handle *cfg,
598 struct ConfigSection *spos;
599 struct ConfigSection *prev;
600 struct ConfigEntry *ent;
603 spos = cfg->sections;
606 if (0 == strcasecmp (section, spos->name))
609 cfg->sections = spos->next;
611 prev->next = spos->next;
612 while (NULL != (ent = spos->entries))
614 spos->entries = ent->next;
615 GNUNET_free (ent->key);
616 GNUNET_free_non_null (ent->val);
618 cfg->dirty = GNUNET_YES;
620 GNUNET_free (spos->name);
631 * Copy a configuration value to the given target configuration.
632 * Overwrites existing entries.
634 * @param cls the destination configuration (struct GNUNET_CONFIGURATION_Handle*)
635 * @param section section for the value
636 * @param option option name of the value
637 * @param value value to copy
640 copy_entry (void *cls, const char *section, const char *option,
643 struct GNUNET_CONFIGURATION_Handle *dst = cls;
645 GNUNET_CONFIGURATION_set_value_string (dst, section, option, value);
650 * Duplicate an existing configuration object.
652 * @param cfg configuration to duplicate
653 * @return duplicate configuration
655 struct GNUNET_CONFIGURATION_Handle *
656 GNUNET_CONFIGURATION_dup (const struct GNUNET_CONFIGURATION_Handle *cfg)
658 struct GNUNET_CONFIGURATION_Handle *ret;
660 ret = GNUNET_CONFIGURATION_create ();
661 GNUNET_CONFIGURATION_iterate (cfg, ©_entry, ret);
667 * Find a section entry from a configuration.
670 * @param section FIXME
671 * @return matching entry, NULL if not found
673 static struct ConfigSection *
674 findSection (const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section)
676 struct ConfigSection *pos;
679 while ((pos != NULL) && (0 != strcasecmp (section, pos->name)))
686 * Find an entry from a configuration.
688 * @param cfg handle to the configuration
689 * @param section section the option is in
690 * @param key the option
691 * @return matching entry, NULL if not found
693 static struct ConfigEntry *
694 findEntry (const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section,
697 struct ConfigSection *sec;
698 struct ConfigEntry *pos;
700 if (NULL == (sec = findSection (cfg, section)))
703 while ((pos != NULL) && (0 != strcasecmp (key, pos->key)))
710 * A callback function, compares entries from two configurations
711 * (default against a new configuration) and write the diffs in a
712 * diff-configuration object (the callback object).
714 * @param cls the diff configuration (struct DiffHandle*)
715 * @param section section for the value (of the default conf.)
716 * @param option option name of the value (of the default conf.)
717 * @param value value to copy (of the default conf.)
720 compare_entries (void *cls, const char *section, const char *option,
723 struct DiffHandle *dh = cls;
724 struct ConfigEntry *entNew;
726 entNew = findEntry (dh->cfgDefault, section, option);
727 if ( (NULL != entNew) &&
728 (NULL != entNew->val) &&
729 (0 == strcmp (entNew->val, value)) )
731 GNUNET_CONFIGURATION_set_value_string (dh->cfgDiff, section, option, value);
736 * Compute configuration with only entries that have been changed
738 * @param cfgDefault original configuration
739 * @param cfgNew new configuration
740 * @return configuration with only the differences, never NULL
742 struct GNUNET_CONFIGURATION_Handle *
743 GNUNET_CONFIGURATION_get_diff (const struct GNUNET_CONFIGURATION_Handle
745 const struct GNUNET_CONFIGURATION_Handle
748 struct DiffHandle diffHandle;
750 diffHandle.cfgDiff = GNUNET_CONFIGURATION_create ();
751 diffHandle.cfgDefault = cfgDefault;
752 GNUNET_CONFIGURATION_iterate (cfgNew, &compare_entries, &diffHandle);
753 return diffHandle.cfgDiff;
758 * Write only configuration entries that have been changed to configuration file
759 * @param cfgDefault default configuration
760 * @param cfgNew new configuration
761 * @param filename where to write the configuration diff between default and new
762 * @return GNUNET_OK on success, GNUNET_SYSERR on error
765 GNUNET_CONFIGURATION_write_diffs (const struct GNUNET_CONFIGURATION_Handle
767 const struct GNUNET_CONFIGURATION_Handle
768 *cfgNew, const char *filename)
771 struct GNUNET_CONFIGURATION_Handle *diff;
773 diff = GNUNET_CONFIGURATION_get_diff (cfgDefault, cfgNew);
774 ret = GNUNET_CONFIGURATION_write (diff, filename);
775 GNUNET_CONFIGURATION_destroy (diff);
781 * Set a configuration value that should be a string.
783 * @param cfg configuration to update
784 * @param section section of interest
785 * @param option option of interest
786 * @param value value to set
789 GNUNET_CONFIGURATION_set_value_string (struct GNUNET_CONFIGURATION_Handle *cfg,
790 const char *section, const char *option,
793 struct ConfigSection *sec;
794 struct ConfigEntry *e;
797 e = findEntry (cfg, section, option);
802 GNUNET_free_non_null (e->val);
807 nv = GNUNET_strdup (value);
808 GNUNET_free_non_null (e->val);
813 sec = findSection (cfg, section);
816 sec = GNUNET_new (struct ConfigSection);
817 sec->name = GNUNET_strdup (section);
818 sec->next = cfg->sections;
821 e = GNUNET_new (struct ConfigEntry);
822 e->key = GNUNET_strdup (option);
823 e->val = GNUNET_strdup (value);
824 e->next = sec->entries;
830 * Set a configuration value that should be a number.
832 * @param cfg configuration to update
833 * @param section section of interest
834 * @param option option of interest
835 * @param number value to set
838 GNUNET_CONFIGURATION_set_value_number (struct GNUNET_CONFIGURATION_Handle *cfg,
839 const char *section, const char *option,
840 unsigned long long number)
844 GNUNET_snprintf (s, 64, "%llu", number);
845 GNUNET_CONFIGURATION_set_value_string (cfg, section, option, s);
850 * Get a configuration value that should be a number.
852 * @param cfg configuration to inspect
853 * @param section section of interest
854 * @param option option of interest
855 * @param number where to store the numeric value of the option
856 * @return GNUNET_OK on success, GNUNET_SYSERR on error
859 GNUNET_CONFIGURATION_get_value_number (const struct GNUNET_CONFIGURATION_Handle
860 *cfg, const char *section,
862 unsigned long long *number)
864 struct ConfigEntry *e;
866 if (NULL == (e = findEntry (cfg, section, option)))
867 return GNUNET_SYSERR;
869 return GNUNET_SYSERR;
870 if (1 != SSCANF (e->val, "%llu", number))
871 return GNUNET_SYSERR;
877 * Get a configuration value that should be a relative time.
879 * @param cfg configuration to inspect
880 * @param section section of interest
881 * @param option option of interest
882 * @param time set to the time value stored in the configuration
883 * @return GNUNET_OK on success, GNUNET_SYSERR on error
886 GNUNET_CONFIGURATION_get_value_time (const struct GNUNET_CONFIGURATION_Handle
887 *cfg, const char *section,
889 struct GNUNET_TIME_Relative *time)
891 struct ConfigEntry *e;
893 if (NULL == (e = findEntry (cfg, section, option)))
894 return GNUNET_SYSERR;
896 return GNUNET_SYSERR;
897 return GNUNET_STRINGS_fancy_time_to_relative (e->val, time);
902 * Get a configuration value that should be a size in bytes.
904 * @param cfg configuration to inspect
905 * @param section section of interest
906 * @param option option of interest
907 * @param size set to the size in bytes as stored in the configuration
908 * @return GNUNET_OK on success, GNUNET_SYSERR on error
911 GNUNET_CONFIGURATION_get_value_size (const struct GNUNET_CONFIGURATION_Handle
912 *cfg, const char *section,
914 unsigned long long *size)
916 struct ConfigEntry *e;
918 if (NULL == (e = findEntry (cfg, section, option)))
919 return GNUNET_SYSERR;
921 return GNUNET_SYSERR;
922 return GNUNET_STRINGS_fancy_size_to_bytes (e->val, size);
927 * Get a configuration value that should be a string.
929 * @param cfg configuration to inspect
930 * @param section section of interest
931 * @param option option of interest
932 * @param value will be set to a freshly allocated configuration
933 * value, or NULL if option is not specified
934 * @return GNUNET_OK on success, GNUNET_SYSERR on error
937 GNUNET_CONFIGURATION_get_value_string (const struct GNUNET_CONFIGURATION_Handle
938 *cfg, const char *section,
939 const char *option, char **value)
941 struct ConfigEntry *e;
943 LOG (GNUNET_ERROR_TYPE_DEBUG, "Asked to retrieve string `%s' in section `%s'\n", option, section);
944 if ( (NULL == (e = findEntry (cfg, section, option))) ||
948 return GNUNET_SYSERR;
950 *value = GNUNET_strdup (e->val);
956 * Get a configuration value that should be in a set of
959 * @param cfg configuration to inspect
960 * @param section section of interest
961 * @param option option of interest
962 * @param choices NULL-terminated list of legal values
963 * @param value will be set to an entry in the legal list,
964 * or NULL if option is not specified and no default given
965 * @return GNUNET_OK on success, GNUNET_SYSERR on error
968 GNUNET_CONFIGURATION_get_value_choice (const struct GNUNET_CONFIGURATION_Handle
969 *cfg, const char *section,
970 const char *option, const char **choices,
973 struct ConfigEntry *e;
976 if (NULL == (e = findEntry (cfg, section, option)))
977 return GNUNET_SYSERR;
978 for (i = 0; NULL != choices[i]; i++)
979 if (0 == strcasecmp (choices[i], e->val))
981 if (NULL == choices[i])
983 LOG (GNUNET_ERROR_TYPE_ERROR,
984 _("Configuration value '%s' for '%s'"
985 " in section '%s' is not in set of legal choices\n"), e->val, option,
987 return GNUNET_SYSERR;
995 * Test if we have a value for a particular option
996 * @param cfg configuration to inspect
997 * @param section section of interest
998 * @param option option of interest
999 * @return GNUNET_YES if so, GNUNET_NO if not.
1002 GNUNET_CONFIGURATION_have_value (const struct GNUNET_CONFIGURATION_Handle *cfg,
1003 const char *section, const char *option)
1005 struct ConfigEntry *e;
1007 if ((NULL == (e = findEntry (cfg, section, option))) || (NULL == e->val))
1014 * Expand an expression of the form "$FOO/BAR" to "DIRECTORY/BAR"
1015 * where either in the "PATHS" section or the environtment
1016 * "FOO" is set to "DIRECTORY".
1018 * @param cfg configuration to use for path expansion
1019 * @param orig string to $-expand (will be freed!)
1020 * @return $-expanded string
1023 GNUNET_CONFIGURATION_expand_dollar (const struct GNUNET_CONFIGURATION_Handle
1032 LOG (GNUNET_ERROR_TYPE_DEBUG, "Asked to $-expand %s\n", orig);
1036 LOG (GNUNET_ERROR_TYPE_DEBUG, "Doesn't start with $ - not expanding\n");
1040 while ((orig[i] != '/') && (orig[i] != '\\') && (orig[i] != '\0'))
1042 if (orig[i] == '\0')
1049 post = &orig[i + 1];
1051 LOG (GNUNET_ERROR_TYPE_DEBUG, "Split into `%s' and `%s'\n", orig, post);
1053 GNUNET_CONFIGURATION_get_value_filename (cfg, "PATHS", &orig[1], &prefix))
1055 LOG (GNUNET_ERROR_TYPE_DEBUG, "Filename for `%s' is not in PATHS config section\n", &orig[1]);
1056 if (NULL == (env = getenv (&orig[1])))
1058 LOG (GNUNET_ERROR_TYPE_DEBUG, "`%s' is not an environment variable\n", &orig[1]);
1059 orig[i] = DIR_SEPARATOR;
1060 LOG (GNUNET_ERROR_TYPE_DEBUG, "Expanded to `%s' (returning orig)\n", orig);
1063 prefix = GNUNET_strdup (env);
1065 LOG (GNUNET_ERROR_TYPE_DEBUG, "Prefix is `%s'\n", prefix);
1066 result = GNUNET_malloc (strlen (prefix) + strlen (post) + 2);
1067 strcpy (result, prefix);
1068 if ((strlen (prefix) == 0) ||
1069 ((prefix[strlen (prefix) - 1] != DIR_SEPARATOR) && (strlen (post) > 0)))
1070 strcat (result, DIR_SEPARATOR_STR);
1071 strcat (result, post);
1072 GNUNET_free (prefix);
1074 LOG (GNUNET_ERROR_TYPE_DEBUG, "Expanded to `%s'\n", result);
1080 * Get a configuration value that should be a string.
1082 * @param cfg configuration to inspect
1083 * @param section section of interest
1084 * @param option option of interest
1085 * @param value will be set to a freshly allocated configuration
1086 * value, or NULL if option is not specified
1087 * @return GNUNET_OK on success, GNUNET_SYSERR on error
1090 GNUNET_CONFIGURATION_get_value_filename (const struct
1091 GNUNET_CONFIGURATION_Handle *cfg,
1092 const char *section,
1093 const char *option, char **value)
1097 LOG (GNUNET_ERROR_TYPE_DEBUG, "Asked to retrieve filename `%s' in section `%s'\n", option, section);
1099 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &tmp))
1101 LOG (GNUNET_ERROR_TYPE_DEBUG, "Failed to retrieve filename\n");
1103 return GNUNET_SYSERR;
1105 LOG (GNUNET_ERROR_TYPE_DEBUG, "Retrieved filename `%s', $-expanding\n", tmp);
1106 tmp = GNUNET_CONFIGURATION_expand_dollar (cfg, tmp);
1107 LOG (GNUNET_ERROR_TYPE_DEBUG, "Expanded to filename `%s', *nix-expanding\n", tmp);
1108 *value = GNUNET_STRINGS_filename_expand (tmp);
1110 LOG (GNUNET_ERROR_TYPE_DEBUG, "Filename result is `%s'\n", *value);
1112 return GNUNET_SYSERR;
1118 * Get a configuration value that should be in a set of
1119 * "GNUNET_YES" or "GNUNET_NO".
1121 * @param cfg configuration to inspect
1122 * @param section section of interest
1123 * @param option option of interest
1124 * @return GNUNET_YES, GNUNET_NO or GNUNET_SYSERR
1127 GNUNET_CONFIGURATION_get_value_yesno (const struct GNUNET_CONFIGURATION_Handle
1128 *cfg, const char *section,
1131 static const char *yesno[] = { "YES", "NO", NULL };
1136 GNUNET_CONFIGURATION_get_value_choice (cfg, section, option, yesno, &val);
1137 if (ret == GNUNET_SYSERR)
1139 if (val == yesno[0])
1146 * Iterate over the set of filenames stored in a configuration value.
1148 * @param cfg configuration to inspect
1149 * @param section section of interest
1150 * @param option option of interest
1151 * @param cb function to call on each filename
1152 * @param cb_cls closure for cb
1153 * @return number of filenames iterated over, -1 on error
1156 GNUNET_CONFIGURATION_iterate_value_filenames (const struct
1157 GNUNET_CONFIGURATION_Handle *cfg,
1158 const char *section,
1160 GNUNET_FileNameCallback cb,
1170 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &list))
1172 GNUNET_assert (list != NULL);
1177 while (pos[0] == ' ')
1179 if (strlen (pos) == 0)
1182 while ((end[0] != ' ') && (end[0] != '\0'))
1190 memmove (end, &end[1], strlen (&end[1]) + 1);
1192 /* illegal, but just keep it */
1195 /* illegal, but just ignore that there was a '/' */
1203 if (strlen (pos) > 0)
1206 if ((cb != NULL) && (GNUNET_OK != cb (cb_cls, pos)))
1208 ret = GNUNET_SYSERR;
1224 * @param value FIXME
1228 escape_name (const char *value)
1234 escaped = GNUNET_malloc (strlen (value) * 2 + 1);
1235 memset (escaped, 0, strlen (value) * 2 + 1);
1238 while (rpos[0] != '\0')
1261 * @param cls string we compare with (const char*)
1262 * @param fn filename we are currently looking at
1263 * @return GNUNET_OK if the names do not match, GNUNET_SYSERR if they do
1266 test_match (void *cls, const char *fn)
1268 const char *of = cls;
1270 return (0 == strcmp (of, fn)) ? GNUNET_SYSERR : GNUNET_OK;
1275 * Append a filename to a configuration value that
1276 * represents a list of filenames
1278 * @param cfg configuration to update
1279 * @param section section of interest
1280 * @param option option of interest
1281 * @param value filename to append
1282 * @return GNUNET_OK on success,
1283 * GNUNET_NO if the filename already in the list
1284 * GNUNET_SYSERR on error
1287 GNUNET_CONFIGURATION_append_value_filename (struct GNUNET_CONFIGURATION_Handle
1288 *cfg, const char *section,
1296 if (GNUNET_SYSERR ==
1297 GNUNET_CONFIGURATION_iterate_value_filenames (cfg, section, option,
1300 return GNUNET_NO; /* already exists */
1302 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &old))
1303 old = GNUNET_strdup ("");
1304 escaped = escape_name (value);
1305 nw = GNUNET_malloc (strlen (old) + strlen (escaped) + 2);
1307 if (strlen (old) > 0)
1309 strcat (nw, escaped);
1310 GNUNET_CONFIGURATION_set_value_string (cfg, section, option, nw);
1313 GNUNET_free (escaped);
1319 * Remove a filename from a configuration value that
1320 * represents a list of filenames
1322 * @param cfg configuration to update
1323 * @param section section of interest
1324 * @param option option of interest
1325 * @param value filename to remove
1326 * @return GNUNET_OK on success,
1327 * GNUNET_NO if the filename is not in the list,
1328 * GNUNET_SYSERR on error
1331 GNUNET_CONFIGURATION_remove_value_filename (struct GNUNET_CONFIGURATION_Handle
1332 *cfg, const char *section,
1343 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &list))
1345 match = escape_name (value);
1349 while (pos[0] == ' ')
1351 if (strlen (pos) == 0)
1354 while ((end[0] != ' ') && (end[0] != '\0'))
1365 /* illegal, but just keep it */
1368 /* illegal, but just ignore that there was a '/' */
1376 if (0 == strcmp (pos, match))
1379 memmove (pos, &end[1], strlen (&end[1]) + 1);
1387 GNUNET_CONFIGURATION_set_value_string (cfg, section, option, list);
1389 GNUNET_free (match);
1398 GNUNET_free (match);
1404 * Wrapper around GNUNET_CONFIGURATION_parse.
1406 * @param cls the cfg
1407 * @param filename file to parse
1408 * @return GNUNET_OK on success
1411 parse_configuration_file (void *cls, const char *filename)
1413 struct GNUNET_CONFIGURATION_Handle *cfg = cls;
1417 /* Examine file extension */
1418 ext = strrchr (filename, '.');
1419 if ((NULL == ext) || (0 != strcmp (ext, ".conf")))
1421 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Skipping file `%s'\n", filename);
1425 ret = GNUNET_CONFIGURATION_parse (cfg, filename);
1431 * Load default configuration. This function will parse the
1432 * defaults from the given defaults_d directory.
1434 * @param cfg configuration to update
1435 * @param defaults_d directory with the defaults
1436 * @return GNUNET_OK on success, GNUNET_SYSERR on error
1439 GNUNET_CONFIGURATION_load_from (struct GNUNET_CONFIGURATION_Handle *cfg,
1440 const char *defaults_d)
1442 if (GNUNET_SYSERR ==
1443 GNUNET_DISK_directory_scan (defaults_d, &parse_configuration_file, cfg))
1444 return GNUNET_SYSERR; /* no configuration at all found */
1450 * Load configuration (starts with defaults, then loads
1451 * system-specific configuration).
1453 * @param cfg configuration to update
1454 * @param filename name of the configuration file, NULL to load defaults
1455 * @return GNUNET_OK on success, GNUNET_SYSERR on error
1458 GNUNET_CONFIGURATION_load (struct GNUNET_CONFIGURATION_Handle *cfg,
1459 const char *filename)
1464 ipath = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_DATADIR);
1466 return GNUNET_SYSERR;
1468 GNUNET_asprintf (&baseconfig, "%s%s", ipath, "config.d");
1469 GNUNET_free (ipath);
1470 if (GNUNET_SYSERR ==
1471 GNUNET_DISK_directory_scan (baseconfig, &parse_configuration_file, cfg))
1473 GNUNET_free (baseconfig);
1474 return GNUNET_SYSERR; /* no configuration at all found */
1476 GNUNET_free (baseconfig);
1477 if ((filename != NULL) &&
1478 (GNUNET_OK != GNUNET_CONFIGURATION_parse (cfg, filename)))
1480 /* specified configuration not found */
1481 return GNUNET_SYSERR;
1484 GNUNET_CONFIGURATION_have_value (cfg, "PATHS", "DEFAULTCONFIG"))) &&
1486 GNUNET_CONFIGURATION_set_value_string (cfg, "PATHS", "DEFAULTCONFIG",
1493 /* end of configuration.c */