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_malloc (sizeof (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 * Parse a configuration file, add all of the options in the
144 * file to the configuration environment.
146 * @param cfg configuration to update
147 * @param filename name of the configuration file
148 * @return GNUNET_OK on success, GNUNET_SYSERR on error
151 GNUNET_CONFIGURATION_parse (struct GNUNET_CONFIGURATION_Handle *cfg,
152 const char *filename)
166 fn = GNUNET_STRINGS_filename_expand (filename);
168 return GNUNET_SYSERR;
169 dirty = cfg->dirty; /* back up value! */
170 if (NULL == (fp = FOPEN (fn, "r")))
172 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "fopen", fn);
174 return GNUNET_SYSERR;
178 section = GNUNET_strdup ("");
179 memset (line, 0, 256);
181 while (NULL != fgets (line, 255, fp))
184 for (i = 0; i < 255; i++)
187 if (line[0] == '\n' || line[0] == '#' || line[0] == '%' || line[0] == '\r')
190 for (i = 0; (i < 255 && line[i] != 0); i++)
191 if (line[i] != ' ' && line[i] != '\n' && line[i] != '\r')
195 /* remove tailing whitespace */
196 for (i = strlen (line) - 1; (i >= 0) && (isspace ((unsigned char) line[i]));
199 if (1 == SSCANF (line, "@INLINE@ %191[^\n]", value))
202 if (GNUNET_OK != GNUNET_CONFIGURATION_parse (cfg, value))
203 ret = GNUNET_SYSERR; /* failed to parse included config */
205 else if (1 == SSCANF (line, "[%99[^]]]", value))
208 GNUNET_free (section);
209 section = GNUNET_strdup (value);
211 else if (2 == SSCANF (line, " %63[^= ] = %191[^\n]", tag, value))
215 i = strlen (value) - 1;
216 while ((i >= 0) && (isspace ((unsigned char) value[i])))
223 while ((value[i] != '\0') && (value[i] != '"'))
233 GNUNET_CONFIGURATION_set_value_string (cfg, section, tag, &value[i]);
235 else if (1 == SSCANF (line, " %63[^= ] =[^\n]", tag))
238 GNUNET_CONFIGURATION_set_value_string (cfg, section, tag, "");
243 LOG (GNUNET_ERROR_TYPE_WARNING,
244 _("Syntax error in configuration file `%s' at line %u.\n"), filename,
250 GNUNET_assert (0 == FCLOSE (fp));
251 /* restore dirty flag - anything we set in the meantime
254 GNUNET_free (section);
260 * Test if there are configuration options that were
261 * changed since the last save.
263 * @param cfg configuration to inspect
264 * @return GNUNET_NO if clean, GNUNET_YES if dirty, GNUNET_SYSERR on error (i.e. last save failed)
267 GNUNET_CONFIGURATION_is_dirty (const struct GNUNET_CONFIGURATION_Handle *cfg)
274 * Write configuration file.
276 * @param cfg configuration to write
277 * @param filename where to write the configuration
278 * @return GNUNET_OK on success, GNUNET_SYSERR on error
281 GNUNET_CONFIGURATION_write (struct GNUNET_CONFIGURATION_Handle *cfg,
282 const char *filename)
284 struct ConfigSection *sec;
285 struct ConfigEntry *ent;
292 fn = GNUNET_STRINGS_filename_expand (filename);
294 return GNUNET_SYSERR;
295 if (GNUNET_OK != GNUNET_DISK_directory_create_for_file (fn))
298 return GNUNET_SYSERR;
300 if (NULL == (fp = FOPEN (fn, "w")))
302 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "fopen", fn);
304 return GNUNET_SYSERR;
311 if (0 > FPRINTF (fp, "[%s]\n", sec->name))
319 if (ent->val != NULL)
321 val = GNUNET_malloc (strlen (ent->val) * 2 + 1);
322 strcpy (val, ent->val);
323 while (NULL != (pos = strstr (val, "\n")))
325 memmove (&pos[2], &pos[1], strlen (&pos[1]));
329 if (0 > FPRINTF (fp, "%s = %s\n", ent->key, val))
341 if (0 > FPRINTF (fp, "%s\n", ""))
349 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "fprintf", filename);
350 GNUNET_assert (0 == FCLOSE (fp));
353 cfg->dirty = GNUNET_SYSERR; /* last write failed */
354 return GNUNET_SYSERR;
356 cfg->dirty = GNUNET_NO; /* last write succeeded */
362 * Iterate over all options in the configuration.
364 * @param cfg configuration to inspect
365 * @param iter function to call on each option
366 * @param iter_cls closure for iter
369 GNUNET_CONFIGURATION_iterate (const struct GNUNET_CONFIGURATION_Handle *cfg,
370 GNUNET_CONFIGURATION_Iterator iter,
373 struct ConfigSection *spos;
374 struct ConfigEntry *epos;
376 spos = cfg->sections;
379 epos = spos->entries;
382 iter (iter_cls, spos->name, epos->key, epos->val);
391 * Iterate over values of a section in the configuration.
393 * @param cfg configuration to inspect
394 * @param section the section
395 * @param iter function to call on each option
396 * @param iter_cls closure for iter
399 GNUNET_CONFIGURATION_iterate_section_values (const struct
400 GNUNET_CONFIGURATION_Handle *cfg,
402 GNUNET_CONFIGURATION_Iterator iter,
405 struct ConfigSection *spos;
406 struct ConfigEntry *epos;
408 spos = cfg->sections;
409 while ((spos != NULL) && (0 != strcasecmp (spos->name, section)))
415 epos = spos->entries;
418 iter (iter_cls, spos->name, epos->key, epos->val);
425 * Iterate over all sections in the configuration.
427 * @param cfg configuration to inspect
428 * @param iter function to call on each section
429 * @param iter_cls closure for iter
432 GNUNET_CONFIGURATION_iterate_sections (const struct GNUNET_CONFIGURATION_Handle
434 GNUNET_CONFIGURATION_Section_Iterator
435 iter, void *iter_cls)
437 struct ConfigSection *spos;
438 struct ConfigSection *next;
440 next = cfg->sections;
445 iter (iter_cls, spos->name);
450 * Remove the given section and all options in it.
452 * @param cfg configuration to inspect
453 * @param section name of the section to remove
456 GNUNET_CONFIGURATION_remove_section (struct GNUNET_CONFIGURATION_Handle *cfg,
459 struct ConfigSection *spos;
460 struct ConfigSection *prev;
461 struct ConfigEntry *ent;
464 spos = cfg->sections;
467 if (0 == strcasecmp (section, spos->name))
470 cfg->sections = spos->next;
472 prev->next = spos->next;
473 while (NULL != (ent = spos->entries))
475 spos->entries = ent->next;
476 GNUNET_free (ent->key);
477 GNUNET_free_non_null (ent->val);
479 cfg->dirty = GNUNET_YES;
481 GNUNET_free (spos->name);
492 * Copy a configuration value to the given target configuration.
493 * Overwrites existing entries.
495 * @param cls the destination configuration (struct GNUNET_CONFIGURATION_Handle*)
496 * @param section section for the value
497 * @param option option name of the value
498 * @param value value to copy
501 copy_entry (void *cls, const char *section, const char *option,
504 struct GNUNET_CONFIGURATION_Handle *dst = cls;
506 GNUNET_CONFIGURATION_set_value_string (dst, section, option, value);
511 * Duplicate an existing configuration object.
513 * @param cfg configuration to duplicate
514 * @return duplicate configuration
516 struct GNUNET_CONFIGURATION_Handle *
517 GNUNET_CONFIGURATION_dup (const struct GNUNET_CONFIGURATION_Handle *cfg)
519 struct GNUNET_CONFIGURATION_Handle *ret;
521 ret = GNUNET_CONFIGURATION_create ();
522 GNUNET_CONFIGURATION_iterate (cfg, ©_entry, ret);
531 * @param section FIXME
532 * @return matching entry, NULL if not found
534 static struct ConfigSection *
535 findSection (const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section)
537 struct ConfigSection *pos;
540 while ((pos != NULL) && (0 != strcasecmp (section, pos->name)))
547 * Find an entry from a configuration.
549 * @param cfg handle to the configuration
550 * @param section section the option is in
551 * @param key the option
552 * @return matching entry, NULL if not found
554 static struct ConfigEntry *
555 findEntry (const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section,
558 struct ConfigSection *sec;
559 struct ConfigEntry *pos;
561 sec = findSection (cfg, section);
565 while ((pos != NULL) && (0 != strcasecmp (key, pos->key)))
572 * A callback function, compares entries from two configurations
573 * (default against a new configuration) and write the diffs in a
574 * diff-configuration object (the callback object).
576 * @param cls the diff configuration (struct DiffHandle*)
577 * @param section section for the value (of the default conf.)
578 * @param option option name of the value (of the default conf.)
579 * @param value value to copy (of the default conf.)
582 compareEntries (void *cls, const char *section, const char *option,
585 struct DiffHandle *dh = cls;
586 struct ConfigEntry *entNew;
588 entNew = findEntry (dh->cfgDefault, section, option);
589 if ((entNew != NULL) && (strcmp (entNew->val, value) == 0))
591 GNUNET_CONFIGURATION_set_value_string (dh->cfgDiff, section, option, value);
596 * Write only configuration entries that have been changed to configuration file
597 * @param cfgDefault default configuration
598 * @param cfgNew new configuration
599 * @param filename where to write the configuration diff between default and new
600 * @return GNUNET_OK on success, GNUNET_SYSERR on error
603 GNUNET_CONFIGURATION_write_diffs (const struct GNUNET_CONFIGURATION_Handle
605 const struct GNUNET_CONFIGURATION_Handle
606 *cfgNew, const char *filename)
609 struct DiffHandle diffHandle;
611 diffHandle.cfgDiff = GNUNET_CONFIGURATION_create ();
612 diffHandle.cfgDefault = cfgDefault;
613 GNUNET_CONFIGURATION_iterate (cfgNew, compareEntries, &diffHandle);
614 ret = GNUNET_CONFIGURATION_write (diffHandle.cfgDiff, filename);
615 GNUNET_CONFIGURATION_destroy (diffHandle.cfgDiff);
621 * Set a configuration value that should be a string.
623 * @param cfg configuration to update
624 * @param section section of interest
625 * @param option option of interest
626 * @param value value to set
629 GNUNET_CONFIGURATION_set_value_string (struct GNUNET_CONFIGURATION_Handle *cfg,
630 const char *section, const char *option,
633 struct ConfigSection *sec;
634 struct ConfigEntry *e;
637 e = findEntry (cfg, section, option);
640 nv = GNUNET_strdup (value);
641 GNUNET_free_non_null (e->val);
645 sec = findSection (cfg, section);
648 sec = GNUNET_malloc (sizeof (struct ConfigSection));
649 sec->name = GNUNET_strdup (section);
650 sec->next = cfg->sections;
653 e = GNUNET_malloc (sizeof (struct ConfigEntry));
654 e->key = GNUNET_strdup (option);
655 e->val = GNUNET_strdup (value);
656 e->next = sec->entries;
662 * Set a configuration value that should be a number.
664 * @param cfg configuration to update
665 * @param section section of interest
666 * @param option option of interest
667 * @param number value to set
670 GNUNET_CONFIGURATION_set_value_number (struct GNUNET_CONFIGURATION_Handle *cfg,
671 const char *section, const char *option,
672 unsigned long long number)
676 GNUNET_snprintf (s, 64, "%llu", number);
677 GNUNET_CONFIGURATION_set_value_string (cfg, section, option, s);
682 * Get a configuration value that should be a number.
684 * @param cfg configuration to inspect
685 * @param section section of interest
686 * @param option option of interest
687 * @param number where to store the numeric value of the option
688 * @return GNUNET_OK on success, GNUNET_SYSERR on error
691 GNUNET_CONFIGURATION_get_value_number (const struct GNUNET_CONFIGURATION_Handle
692 *cfg, const char *section,
694 unsigned long long *number)
696 struct ConfigEntry *e;
698 e = findEntry (cfg, section, option);
700 return GNUNET_SYSERR;
701 if (1 != SSCANF (e->val, "%llu", number))
702 return GNUNET_SYSERR;
708 * Get a configuration value that should be a relative time.
710 * @param cfg configuration to inspect
711 * @param section section of interest
712 * @param option option of interest
713 * @param time set to the time value stored in the configuration
714 * @return GNUNET_OK on success, GNUNET_SYSERR on error
717 GNUNET_CONFIGURATION_get_value_time (const struct GNUNET_CONFIGURATION_Handle
718 *cfg, const char *section,
720 struct GNUNET_TIME_Relative *time)
722 struct ConfigEntry *e;
724 e = findEntry (cfg, section, option);
726 return GNUNET_SYSERR;
728 return GNUNET_STRINGS_fancy_time_to_relative (e->val, time);
733 * Get a configuration value that should be a size in bytes.
735 * @param cfg configuration to inspect
736 * @param section section of interest
737 * @param option option of interest
738 * @param size set to the size in bytes as stored in the configuration
739 * @return GNUNET_OK on success, GNUNET_SYSERR on error
742 GNUNET_CONFIGURATION_get_value_size (const struct GNUNET_CONFIGURATION_Handle
743 *cfg, const char *section,
745 unsigned long long *size)
747 struct ConfigEntry *e;
749 e = findEntry (cfg, section, option);
751 return GNUNET_SYSERR;
752 return GNUNET_STRINGS_fancy_size_to_bytes (e->val, size);
757 * Get a configuration value that should be a string.
759 * @param cfg configuration to inspect
760 * @param section section of interest
761 * @param option option of interest
762 * @param value will be set to a freshly allocated configuration
763 * value, or NULL if option is not specified
764 * @return GNUNET_OK on success, GNUNET_SYSERR on error
767 GNUNET_CONFIGURATION_get_value_string (const struct GNUNET_CONFIGURATION_Handle
768 *cfg, const char *section,
769 const char *option, char **value)
771 struct ConfigEntry *e;
773 e = findEntry (cfg, section, option);
774 if ((e == NULL) || (e->val == NULL))
777 return GNUNET_SYSERR;
779 *value = GNUNET_strdup (e->val);
785 * Get a configuration value that should be in a set of
788 * @param cfg configuration to inspect
789 * @param section section of interest
790 * @param option option of interest
791 * @param choices NULL-terminated list of legal values
792 * @param value will be set to an entry in the legal list,
793 * or NULL if option is not specified and no default given
794 * @return GNUNET_OK on success, GNUNET_SYSERR on error
797 GNUNET_CONFIGURATION_get_value_choice (const struct GNUNET_CONFIGURATION_Handle
798 *cfg, const char *section,
799 const char *option, const char **choices,
802 struct ConfigEntry *e;
805 e = findEntry (cfg, section, option);
807 return GNUNET_SYSERR;
809 while (choices[i] != NULL)
811 if (0 == strcasecmp (choices[i], e->val))
815 if (choices[i] == NULL)
817 LOG (GNUNET_ERROR_TYPE_ERROR,
818 _("Configuration value '%s' for '%s'"
819 " in section '%s' is not in set of legal choices\n"), e->val, option,
821 return GNUNET_SYSERR;
829 * Test if we have a value for a particular option
830 * @param cfg configuration to inspect
831 * @param section section of interest
832 * @param option option of interest
833 * @return GNUNET_YES if so, GNUNET_NO if not.
836 GNUNET_CONFIGURATION_have_value (const struct GNUNET_CONFIGURATION_Handle *cfg,
837 const char *section, const char *option)
839 struct ConfigEntry *e;
841 if ((NULL == (e = findEntry (cfg, section, option))) || (e->val == NULL))
848 * Expand an expression of the form "$FOO/BAR" to "DIRECTORY/BAR"
849 * where either in the "PATHS" section or the environtment
850 * "FOO" is set to "DIRECTORY".
852 * @param cfg configuration to use for path expansion
853 * @param orig string to $-expand (will be freed!)
854 * @return $-expanded string
857 GNUNET_CONFIGURATION_expand_dollar (const struct GNUNET_CONFIGURATION_Handle
869 while ((orig[i] != '/') && (orig[i] != '\\') && (orig[i] != '\0'))
881 GNUNET_CONFIGURATION_get_value_filename (cfg, "PATHS", &orig[1], &prefix))
883 if (NULL == (env = getenv (&orig[1])))
885 orig[i] = DIR_SEPARATOR;
888 prefix = GNUNET_strdup (env);
890 result = GNUNET_malloc (strlen (prefix) + strlen (post) + 2);
891 strcpy (result, prefix);
892 if ((strlen (prefix) == 0) ||
893 ((prefix[strlen (prefix) - 1] != DIR_SEPARATOR) && (strlen (post) > 0)))
894 strcat (result, DIR_SEPARATOR_STR);
895 strcat (result, post);
896 GNUNET_free (prefix);
903 * Get a configuration value that should be a string.
905 * @param cfg configuration to inspect
906 * @param section section of interest
907 * @param option option of interest
908 * @param value will be set to a freshly allocated configuration
909 * value, or NULL if option is not specified
910 * @return GNUNET_OK on success, GNUNET_SYSERR on error
913 GNUNET_CONFIGURATION_get_value_filename (const struct
914 GNUNET_CONFIGURATION_Handle *cfg,
916 const char *option, char **value)
921 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &tmp))
924 return GNUNET_SYSERR;
926 tmp = GNUNET_CONFIGURATION_expand_dollar (cfg, tmp);
927 *value = GNUNET_STRINGS_filename_expand (tmp);
930 return GNUNET_SYSERR;
936 * Get a configuration value that should be in a set of
937 * "GNUNET_YES" or "GNUNET_NO".
939 * @param cfg configuration to inspect
940 * @param section section of interest
941 * @param option option of interest
942 * @return GNUNET_YES, GNUNET_NO or GNUNET_SYSERR
945 GNUNET_CONFIGURATION_get_value_yesno (const struct GNUNET_CONFIGURATION_Handle
946 *cfg, const char *section,
949 static const char *yesno[] = { "YES", "NO", NULL };
954 GNUNET_CONFIGURATION_get_value_choice (cfg, section, option, yesno, &val);
955 if (ret == GNUNET_SYSERR)
964 * Iterate over the set of filenames stored in a configuration value.
966 * @param cfg configuration to inspect
967 * @param section section of interest
968 * @param option option of interest
969 * @param cb function to call on each filename
970 * @param cb_cls closure for cb
971 * @return number of filenames iterated over, -1 on error
974 GNUNET_CONFIGURATION_iterate_value_filenames (const struct
975 GNUNET_CONFIGURATION_Handle *cfg,
978 GNUNET_FileNameCallback cb,
988 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &list))
990 GNUNET_assert (list != NULL);
995 while (pos[0] == ' ')
997 if (strlen (pos) == 0)
1000 while ((end[0] != ' ') && (end[0] != '\0'))
1008 memmove (end, &end[1], strlen (&end[1]) + 1);
1010 /* illegal, but just keep it */
1013 /* illegal, but just ignore that there was a '/' */
1021 if (strlen (pos) > 0)
1024 if ((cb != NULL) && (GNUNET_OK != cb (cb_cls, pos)))
1026 ret = GNUNET_SYSERR;
1042 * @param value FIXME
1046 escape_name (const char *value)
1052 escaped = GNUNET_malloc (strlen (value) * 2 + 1);
1053 memset (escaped, 0, strlen (value) * 2 + 1);
1056 while (rpos[0] != '\0')
1079 * @param cls string we compare with (const char*)
1080 * @param fn filename we are currently looking at
1081 * @return GNUNET_OK if the names do not match, GNUNET_SYSERR if they do
1084 test_match (void *cls, const char *fn)
1086 const char *of = cls;
1088 return (0 == strcmp (of, fn)) ? GNUNET_SYSERR : GNUNET_OK;
1093 * Append a filename to a configuration value that
1094 * represents a list of filenames
1096 * @param cfg configuration to update
1097 * @param section section of interest
1098 * @param option option of interest
1099 * @param value filename to append
1100 * @return GNUNET_OK on success,
1101 * GNUNET_NO if the filename already in the list
1102 * GNUNET_SYSERR on error
1105 GNUNET_CONFIGURATION_append_value_filename (struct GNUNET_CONFIGURATION_Handle
1106 *cfg, const char *section,
1114 if (GNUNET_SYSERR ==
1115 GNUNET_CONFIGURATION_iterate_value_filenames (cfg, section, option,
1118 return GNUNET_NO; /* already exists */
1120 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &old))
1121 old = GNUNET_strdup ("");
1122 escaped = escape_name (value);
1123 nw = GNUNET_malloc (strlen (old) + strlen (escaped) + 2);
1125 if (strlen (old) > 0)
1127 strcat (nw, escaped);
1128 GNUNET_CONFIGURATION_set_value_string (cfg, section, option, nw);
1131 GNUNET_free (escaped);
1137 * Remove a filename from a configuration value that
1138 * represents a list of filenames
1140 * @param cfg configuration to update
1141 * @param section section of interest
1142 * @param option option of interest
1143 * @param value filename to remove
1144 * @return GNUNET_OK on success,
1145 * GNUNET_NO if the filename is not in the list,
1146 * GNUNET_SYSERR on error
1149 GNUNET_CONFIGURATION_remove_value_filename (struct GNUNET_CONFIGURATION_Handle
1150 *cfg, const char *section,
1161 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &list))
1163 match = escape_name (value);
1167 while (pos[0] == ' ')
1169 if (strlen (pos) == 0)
1172 while ((end[0] != ' ') && (end[0] != '\0'))
1183 /* illegal, but just keep it */
1186 /* illegal, but just ignore that there was a '/' */
1194 if (0 == strcmp (pos, match))
1197 memmove (pos, &end[1], strlen (&end[1]) + 1);
1205 GNUNET_CONFIGURATION_set_value_string (cfg, section, option, list);
1207 GNUNET_free (match);
1216 GNUNET_free (match);
1222 * Wrapper around GNUNET_CONFIGURATION_parse.
1224 * @param cls the cfg
1225 * @param filename file to parse
1226 * @return GNUNET_OK on success
1229 parse_configuration_file (void *cls, const char *filename)
1231 struct GNUNET_CONFIGURATION_Handle *cfg = cls;
1235 /* Examine file extension */
1236 ext = strrchr (filename, '.');
1237 if ((NULL == ext) || (0 != strcmp (ext, ".conf")))
1239 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Skipping file `%s'\n", filename);
1243 ret = GNUNET_CONFIGURATION_parse (cfg, filename);
1249 * Load default configuration. This function will parse the
1250 * defaults from the given defaults_d directory.
1252 * @param cfg configuration to update
1253 * @param defaults_d directory with the defaults
1254 * @return GNUNET_OK on success, GNUNET_SYSERR on error
1257 GNUNET_CONFIGURATION_load_from (struct GNUNET_CONFIGURATION_Handle *cfg,
1258 const char *defaults_d)
1260 if (GNUNET_SYSERR ==
1261 GNUNET_DISK_directory_scan (defaults_d, &parse_configuration_file, cfg))
1262 return GNUNET_SYSERR; /* no configuration at all found */
1268 * Load configuration (starts with defaults, then loads
1269 * system-specific configuration).
1271 * @param cfg configuration to update
1272 * @param filename name of the configuration file, NULL to load defaults
1273 * @return GNUNET_OK on success, GNUNET_SYSERR on error
1276 GNUNET_CONFIGURATION_load (struct GNUNET_CONFIGURATION_Handle *cfg,
1277 const char *filename)
1282 ipath = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_DATADIR);
1284 return GNUNET_SYSERR;
1286 GNUNET_asprintf (&baseconfig, "%s%s", ipath, "config.d");
1287 GNUNET_free (ipath);
1288 if (GNUNET_SYSERR ==
1289 GNUNET_DISK_directory_scan (baseconfig, &parse_configuration_file, cfg))
1291 GNUNET_free (baseconfig);
1292 return GNUNET_SYSERR; /* no configuration at all found */
1294 GNUNET_free (baseconfig);
1295 if ((filename != NULL) &&
1296 (GNUNET_OK != GNUNET_CONFIGURATION_parse (cfg, filename)))
1298 /* specified configuration not found */
1299 return GNUNET_SYSERR;
1302 GNUNET_CONFIGURATION_have_value (cfg, "PATHS", "DEFAULTCONFIG"))) &&
1304 GNUNET_CONFIGURATION_set_value_string (cfg, "PATHS", "DEFAULTCONFIG",
1307 GNUNET_CONFIGURATION_have_value (cfg, "TESTING", "WEAKRANDOM")) &&
1309 GNUNET_CONFIGURATION_get_value_yesno (cfg, "TESTING", "WEAKRANDOM")))
1310 GNUNET_CRYPTO_random_disable_entropy_gathering ();
1316 /* end of configuration.c */