Merge branch 'master'
[oweals/gnunet.git] / src / util / configuration.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2006, 2007, 2008, 2009, 2013 GNUnet e.V.
4
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.
9
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.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18      Boston, MA 02110-1301, USA.
19 */
20
21 /**
22  * @file src/util/configuration.c
23  * @brief configuration management
24  * @author Christian Grothoff
25  */
26
27 #include "platform.h"
28 #include "gnunet_crypto_lib.h"
29 #include "gnunet_strings_lib.h"
30 #include "gnunet_configuration_lib.h"
31 #include "gnunet_disk_lib.h"
32
33 #define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
34
35 #define LOG_STRERROR_FILE(kind,syscall,filename) GNUNET_log_from_strerror_file (kind, "util", syscall, filename)
36
37 /**
38  * @brief configuration entry
39  */
40 struct ConfigEntry
41 {
42
43   /**
44    * This is a linked list.
45    */
46   struct ConfigEntry *next;
47
48   /**
49    * key for this entry
50    */
51   char *key;
52
53   /**
54    * current, commited value
55    */
56   char *val;
57 };
58
59
60 /**
61  * @brief configuration section
62  */
63 struct ConfigSection
64 {
65   /**
66    * This is a linked list.
67    */
68   struct ConfigSection *next;
69
70   /**
71    * entries in the section
72    */
73   struct ConfigEntry *entries;
74
75   /**
76    * name of the section
77    */
78   char *name;
79 };
80
81
82 /**
83  * @brief configuration data
84  */
85 struct GNUNET_CONFIGURATION_Handle
86 {
87   /**
88    * Configuration sections.
89    */
90   struct ConfigSection *sections;
91
92   /**
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)
96    */
97   int dirty;
98
99 };
100
101
102 /**
103  * Used for diffing a configuration object against
104  * the default one
105  */
106 struct DiffHandle
107 {
108   const struct GNUNET_CONFIGURATION_Handle *cfg_default;
109
110   struct GNUNET_CONFIGURATION_Handle *cfgDiff;
111 };
112
113
114 /**
115  * Create a GNUNET_CONFIGURATION_Handle.
116  *
117  * @return fresh configuration object
118  */
119 struct GNUNET_CONFIGURATION_Handle *
120 GNUNET_CONFIGURATION_create ()
121 {
122   return GNUNET_new (struct GNUNET_CONFIGURATION_Handle);
123 }
124
125
126 /**
127  * Destroy configuration object.
128  *
129  * @param cfg configuration to destroy
130  */
131 void
132 GNUNET_CONFIGURATION_destroy (struct GNUNET_CONFIGURATION_Handle *cfg)
133 {
134   struct ConfigSection *sec;
135
136   while (NULL != (sec = cfg->sections))
137     GNUNET_CONFIGURATION_remove_section (cfg, sec->name);
138   GNUNET_free (cfg);
139 }
140
141
142 /**
143  * De-serializes configuration
144  *
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_SYSERR on error
152  */
153 int
154 GNUNET_CONFIGURATION_deserialize (struct GNUNET_CONFIGURATION_Handle *cfg,
155                                   const char *mem,
156                                   size_t size,
157                                   int allow_inline)
158 {
159   char *line;
160   char *line_orig;
161   size_t line_size;
162   char *pos;
163   unsigned int nr;
164   size_t r_bytes;
165   size_t to_read;
166   size_t i;
167   int emptyline;
168   int ret;
169   char *section;
170   char *eq;
171   char *tag;
172   char *value;
173
174   ret = GNUNET_OK;
175   section = GNUNET_strdup ("");
176   nr = 0;
177   r_bytes = 0;
178   line_orig = NULL;
179   while (r_bytes < size)
180   {
181     GNUNET_free_non_null (line_orig);
182     /* fgets-like behaviour on buffer */
183     to_read = size - r_bytes;
184     pos = memchr (&mem[r_bytes], '\n', to_read);
185     if (NULL == pos)
186     {
187       line_orig = GNUNET_strndup (&mem[r_bytes], line_size = to_read);
188       r_bytes += line_size;
189     }
190     else
191     {
192       line_orig = GNUNET_strndup (&mem[r_bytes], line_size = (pos - &mem[r_bytes]));
193       r_bytes += line_size + 1;
194     }
195     line = line_orig;
196     /* increment line number */
197     nr++;
198     /* tabs and '\r' are whitespace */
199     emptyline = GNUNET_YES;
200     for (i = 0; i < line_size; i++)
201     {
202       if (line[i] == '\t')
203         line[i] = ' ';
204       if (line[i] == '\r')
205         line[i] = ' ';
206       if (' ' != line[i])
207         emptyline = GNUNET_NO;
208     }
209     /* ignore empty lines */
210     if (GNUNET_YES == emptyline)
211       continue;
212
213     /* remove tailing whitespace */
214     for (i = line_size - 1; (i >= 1) && (isspace ((unsigned char) line[i]));i--)
215       line[i] = '\0';
216
217     /* remove leading whitespace */
218     for (; line[0] != '\0' && (isspace ((unsigned char) line[0])); line++);
219
220     /* ignore comments */
221     if ( ('#' == line[0]) || ('%' == line[0]) )
222       continue;
223
224     /* handle special "@INLINE@" directive */
225     if (0 == strncasecmp (line,
226                           "@INLINE@ ",
227                           strlen ("@INLINE@ ")))
228     {
229       /* @INLINE@ value */
230       value = &line[strlen ("@INLINE@ ")];
231       if (GNUNET_YES == allow_inline)
232       {
233         if (GNUNET_OK != GNUNET_CONFIGURATION_parse (cfg, value))
234         {
235           ret = GNUNET_SYSERR;    /* failed to parse included config */
236           break;
237         }
238       }
239       else
240       {
241         LOG (GNUNET_ERROR_TYPE_DEBUG,
242              "Ignoring parsing @INLINE@ configurations, not allowed!\n");
243         ret = GNUNET_SYSERR;
244         break;
245       }
246       continue;
247     }
248     if ( ('[' == line[0]) && (']' == line[line_size - 1]) )
249     {
250       /* [value] */
251       line[line_size - 1] = '\0';
252       value = &line[1];
253       GNUNET_free (section);
254       section = GNUNET_strdup (value);
255       continue;
256     }
257     if (NULL != (eq = strchr (line, '=')))
258     {
259       /* tag = value */
260       tag = GNUNET_strndup (line, eq - line);
261       /* remove tailing whitespace */
262       for (i = strlen (tag) - 1; (i >= 1) && (isspace ((unsigned char) tag[i]));i--)
263         tag[i] = '\0';
264
265       /* Strip whitespace */
266       value = eq + 1;
267       while (isspace ((unsigned char) value[0]))
268         value++;
269       for (i = strlen (value) - 1; (i >= 1) && (isspace ((unsigned char) value[i]));i--)
270         value[i] = '\0';
271
272       /* remove quotes */
273       i = 0;
274       if ( ('"' == value[0]) &&
275            ('"' == value[strlen (value) - 1]) )
276       {
277         value[strlen (value) - 1] = '\0';
278         value++;
279       }
280       GNUNET_CONFIGURATION_set_value_string (cfg, section, tag, &value[i]);
281       GNUNET_free (tag);
282       continue;
283     }
284     /* parse error */
285     LOG (GNUNET_ERROR_TYPE_WARNING,
286          _("Syntax error while deserializing in line %u\n"),
287          nr);
288     ret = GNUNET_SYSERR;
289     break;
290   }
291   GNUNET_free_non_null (line_orig);
292   GNUNET_free (section);
293   GNUNET_assert ( (GNUNET_OK != ret) || (r_bytes == size) );
294   return ret;
295 }
296
297
298 /**
299  * Parse a configuration file, add all of the options in the
300  * file to the configuration environment.
301  *
302  * @param cfg configuration to update
303  * @param filename name of the configuration file
304  * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
305  */
306 int
307 GNUNET_CONFIGURATION_parse (struct GNUNET_CONFIGURATION_Handle *cfg,
308                             const char *filename)
309 {
310   uint64_t fs64;
311   size_t fs;
312   char *fn;
313   char *mem;
314   int dirty;
315   int ret;
316
317   fn = GNUNET_STRINGS_filename_expand (filename);
318   LOG (GNUNET_ERROR_TYPE_DEBUG,
319        "Asked to parse config file `%s'\n",
320        fn);
321   if (NULL == fn)
322     return GNUNET_SYSERR;
323   dirty = cfg->dirty;           /* back up value! */
324   if (GNUNET_SYSERR ==
325       GNUNET_DISK_file_size (fn, &fs64, GNUNET_YES, GNUNET_YES))
326   {
327     LOG (GNUNET_ERROR_TYPE_WARNING,
328          "Error while determining the file size of `%s'\n",
329          fn);
330     GNUNET_free (fn);
331     return GNUNET_SYSERR;
332   }
333   if (fs64 > SIZE_MAX)
334   {
335     GNUNET_break (0);           /* File size is more than the heap size */
336     GNUNET_free (fn);
337     return GNUNET_SYSERR;
338   }
339   fs = fs64;
340   mem = GNUNET_malloc (fs);
341   if (fs != GNUNET_DISK_fn_read (fn, mem, fs))
342   {
343     LOG (GNUNET_ERROR_TYPE_WARNING,
344          _("Error while reading file `%s'\n"),
345          fn);
346     GNUNET_free (fn);
347     GNUNET_free (mem);
348     return GNUNET_SYSERR;
349   }
350   LOG (GNUNET_ERROR_TYPE_DEBUG,
351        "Deserializing contents of file `%s'\n",
352        fn);
353   GNUNET_free (fn);
354   ret = GNUNET_CONFIGURATION_deserialize (cfg, mem, fs, GNUNET_YES);
355   GNUNET_free (mem);
356   /* restore dirty flag - anything we set in the meantime
357    * came from disk */
358   cfg->dirty = dirty;
359   return ret;
360 }
361
362
363 /**
364  * Test if there are configuration options that were
365  * changed since the last save.
366  *
367  * @param cfg configuration to inspect
368  * @return #GNUNET_NO if clean, #GNUNET_YES if dirty, #GNUNET_SYSERR on error (i.e. last save failed)
369  */
370 int
371 GNUNET_CONFIGURATION_is_dirty (const struct GNUNET_CONFIGURATION_Handle *cfg)
372 {
373   return cfg->dirty;
374 }
375
376
377 /**
378  * Serializes the given configuration.
379  *
380  * @param cfg configuration to serialize
381  * @param size will be set to the size of the serialized memory block
382  * @return the memory block where the serialized configuration is
383  *           present. This memory should be freed by the caller
384  */
385 char *
386 GNUNET_CONFIGURATION_serialize (const struct GNUNET_CONFIGURATION_Handle *cfg,
387                                 size_t *size)
388 {
389   struct ConfigSection *sec;
390   struct ConfigEntry *ent;
391   char *mem;
392   char *cbuf;
393   char *val;
394   char *pos;
395   int len;
396   size_t m_size;
397   size_t c_size;
398
399
400   /* Pass1 : calculate the buffer size required */
401   m_size = 0;
402   for (sec = cfg->sections; NULL != sec; sec = sec->next)
403   {
404     /* For each section we need to add 3 charaters: {'[',']','\n'} */
405     m_size += strlen (sec->name) + 3;
406     for (ent = sec->entries; NULL != ent; ent = ent->next)
407     {
408       if (NULL != ent->val)
409       {
410         /* if val has any '\n' then they occupy +1 character as '\n'->'\\','n' */
411         pos = ent->val;
412         while (NULL != (pos = strstr (pos, "\n")))
413         {
414           m_size++;
415           pos++;
416         }
417         /* For each key = value pair we need to add 4 characters (2
418            spaces and 1 equal-to character and 1 new line) */
419         m_size += strlen (ent->key) + strlen (ent->val) + 4;
420       }
421     }
422     /* A new line after section end */
423     m_size++;
424   }
425
426   /* Pass2: Allocate memory and write the configuration to it */
427   mem = GNUNET_malloc (m_size);
428   sec = cfg->sections;
429   c_size = 0;
430   *size = c_size;
431   while (NULL != sec)
432   {
433     len = GNUNET_asprintf (&cbuf, "[%s]\n", sec->name);
434     GNUNET_assert (0 < len);
435     GNUNET_memcpy (mem + c_size, cbuf, len);
436     c_size += len;
437     GNUNET_free (cbuf);
438     for (ent = sec->entries; NULL != ent; ent = ent->next)
439     {
440       if (NULL != ent->val)
441       {
442         val = GNUNET_malloc (strlen (ent->val) * 2 + 1);
443         strcpy (val, ent->val);
444         while (NULL != (pos = strstr (val, "\n")))
445         {
446           memmove (&pos[2], &pos[1], strlen (&pos[1]));
447           pos[0] = '\\';
448           pos[1] = 'n';
449         }
450         len = GNUNET_asprintf (&cbuf, "%s = %s\n", ent->key, val);
451         GNUNET_free (val);
452         GNUNET_memcpy (mem + c_size, cbuf, len);
453         c_size += len;
454         GNUNET_free (cbuf);
455       }
456     }
457     GNUNET_memcpy (mem + c_size, "\n", 1);
458     c_size ++;
459     sec = sec->next;
460   }
461   GNUNET_assert (c_size == m_size);
462   *size = c_size;
463   return mem;
464 }
465
466
467 /**
468  * Write configuration file.
469  *
470  * @param cfg configuration to write
471  * @param filename where to write the configuration
472  * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
473  */
474 int
475 GNUNET_CONFIGURATION_write (struct GNUNET_CONFIGURATION_Handle *cfg,
476                             const char *filename)
477 {
478   char *fn;
479   char *cfg_buf;
480   size_t size;
481
482   fn = GNUNET_STRINGS_filename_expand (filename);
483   if (fn == NULL)
484     return GNUNET_SYSERR;
485   if (GNUNET_OK != GNUNET_DISK_directory_create_for_file (fn))
486   {
487     GNUNET_free (fn);
488     return GNUNET_SYSERR;
489   }
490   cfg_buf = GNUNET_CONFIGURATION_serialize (cfg, &size);
491   if (size != GNUNET_DISK_fn_write (fn, cfg_buf, size,
492                                     GNUNET_DISK_PERM_USER_READ
493                                     | GNUNET_DISK_PERM_USER_WRITE
494                                     | GNUNET_DISK_PERM_GROUP_READ
495                                     | GNUNET_DISK_PERM_GROUP_WRITE))
496   {
497     GNUNET_free (fn);
498     GNUNET_free (cfg_buf);
499     LOG (GNUNET_ERROR_TYPE_WARNING,
500          "Writing configration to file `%s' failed\n",
501          filename);
502     cfg->dirty = GNUNET_SYSERR; /* last write failed */
503     return GNUNET_SYSERR;
504   }
505   GNUNET_free (fn);
506   GNUNET_free (cfg_buf);
507   cfg->dirty = GNUNET_NO;       /* last write succeeded */
508   return GNUNET_OK;
509 }
510
511
512 /**
513  * Iterate over all options in the configuration.
514  *
515  * @param cfg configuration to inspect
516  * @param iter function to call on each option
517  * @param iter_cls closure for @a iter
518  */
519 void
520 GNUNET_CONFIGURATION_iterate (const struct GNUNET_CONFIGURATION_Handle *cfg,
521                               GNUNET_CONFIGURATION_Iterator iter,
522                               void *iter_cls)
523 {
524   struct ConfigSection *spos;
525   struct ConfigEntry *epos;
526
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);
531 }
532
533
534 /**
535  * Iterate over values of a section in the configuration.
536  *
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 @a iter
541  */
542 void
543 GNUNET_CONFIGURATION_iterate_section_values (const struct
544                                              GNUNET_CONFIGURATION_Handle *cfg,
545                                              const char *section,
546                                              GNUNET_CONFIGURATION_Iterator iter,
547                                              void *iter_cls)
548 {
549   struct ConfigSection *spos;
550   struct ConfigEntry *epos;
551
552   spos = cfg->sections;
553   while ((spos != NULL) && (0 != strcasecmp (spos->name, section)))
554     spos = spos->next;
555   if (NULL == spos)
556     return;
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);
560 }
561
562
563 /**
564  * Iterate over all sections in the configuration.
565  *
566  * @param cfg configuration to inspect
567  * @param iter function to call on each section
568  * @param iter_cls closure for @a iter
569  */
570 void
571 GNUNET_CONFIGURATION_iterate_sections (const struct GNUNET_CONFIGURATION_Handle *cfg,
572                                        GNUNET_CONFIGURATION_Section_Iterator iter,
573                                        void *iter_cls)
574 {
575   struct ConfigSection *spos;
576   struct ConfigSection *next;
577
578   next = cfg->sections;
579   while (next != NULL)
580   {
581     spos = next;
582     next = spos->next;
583     iter (iter_cls, spos->name);
584   }
585 }
586
587
588 /**
589  * Remove the given section and all options in it.
590  *
591  * @param cfg configuration to inspect
592  * @param section name of the section to remove
593  */
594 void
595 GNUNET_CONFIGURATION_remove_section (struct GNUNET_CONFIGURATION_Handle *cfg,
596                                      const char *section)
597 {
598   struct ConfigSection *spos;
599   struct ConfigSection *prev;
600   struct ConfigEntry *ent;
601
602   prev = NULL;
603   spos = cfg->sections;
604   while (NULL != spos)
605   {
606     if (0 == strcasecmp (section, spos->name))
607     {
608       if (NULL == prev)
609         cfg->sections = spos->next;
610       else
611         prev->next = spos->next;
612       while (NULL != (ent = spos->entries))
613       {
614         spos->entries = ent->next;
615         GNUNET_free (ent->key);
616         GNUNET_free_non_null (ent->val);
617         GNUNET_free (ent);
618         cfg->dirty = GNUNET_YES;
619       }
620       GNUNET_free (spos->name);
621       GNUNET_free (spos);
622       return;
623     }
624     prev = spos;
625     spos = spos->next;
626   }
627 }
628
629
630 /**
631  * Copy a configuration value to the given target configuration.
632  * Overwrites existing entries.
633  *
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
638  */
639 static void
640 copy_entry (void *cls,
641             const char *section,
642             const char *option,
643             const char *value)
644 {
645   struct GNUNET_CONFIGURATION_Handle *dst = cls;
646
647   GNUNET_CONFIGURATION_set_value_string (dst, section, option, value);
648 }
649
650
651 /**
652  * Duplicate an existing configuration object.
653  *
654  * @param cfg configuration to duplicate
655  * @return duplicate configuration
656  */
657 struct GNUNET_CONFIGURATION_Handle *
658 GNUNET_CONFIGURATION_dup (const struct GNUNET_CONFIGURATION_Handle *cfg)
659 {
660   struct GNUNET_CONFIGURATION_Handle *ret;
661
662   ret = GNUNET_CONFIGURATION_create ();
663   GNUNET_CONFIGURATION_iterate (cfg, &copy_entry, ret);
664   return ret;
665 }
666
667
668 /**
669  * Find a section entry from a configuration.
670  *
671  * @param cfg configuration to search in
672  * @param section name of the section to look for
673  * @return matching entry, NULL if not found
674  */
675 static struct ConfigSection *
676 find_section (const struct GNUNET_CONFIGURATION_Handle *cfg,
677               const char *section)
678 {
679   struct ConfigSection *pos;
680
681   pos = cfg->sections;
682   while ((pos != NULL) && (0 != strcasecmp (section, pos->name)))
683     pos = pos->next;
684   return pos;
685 }
686
687
688 /**
689  * Find an entry from a configuration.
690  *
691  * @param cfg handle to the configuration
692  * @param section section the option is in
693  * @param key the option
694  * @return matching entry, NULL if not found
695  */
696 static struct ConfigEntry *
697 find_entry (const struct GNUNET_CONFIGURATION_Handle *cfg,
698            const char *section,
699            const char *key)
700 {
701   struct ConfigSection *sec;
702   struct ConfigEntry *pos;
703
704   if (NULL == (sec = find_section (cfg, section)))
705     return NULL;
706   pos = sec->entries;
707   while ((pos != NULL) && (0 != strcasecmp (key, pos->key)))
708     pos = pos->next;
709   return pos;
710 }
711
712
713 /**
714  * A callback function, compares entries from two configurations
715  * (default against a new configuration) and write the diffs in a
716  * diff-configuration object (the callback object).
717  *
718  * @param cls the diff configuration (`struct DiffHandle *`)
719  * @param section section for the value (of the default conf.)
720  * @param option option name of the value (of the default conf.)
721  * @param value value to copy (of the default conf.)
722  */
723 static void
724 compare_entries (void *cls,
725                  const char *section,
726                  const char *option,
727                  const char *value)
728 {
729   struct DiffHandle *dh = cls;
730   struct ConfigEntry *entNew;
731
732   entNew = find_entry (dh->cfg_default, section, option);
733   if ( (NULL != entNew) &&
734        (NULL != entNew->val) &&
735        (0 == strcmp (entNew->val, value)) )
736     return;
737   GNUNET_CONFIGURATION_set_value_string (dh->cfgDiff, section, option, value);
738 }
739
740
741 /**
742  * Compute configuration with only entries that have been changed
743  *
744  * @param cfg_default original configuration
745  * @param cfg_new new configuration
746  * @return configuration with only the differences, never NULL
747  */
748 struct GNUNET_CONFIGURATION_Handle *
749 GNUNET_CONFIGURATION_get_diff (const struct GNUNET_CONFIGURATION_Handle *cfg_default,
750                                const struct GNUNET_CONFIGURATION_Handle *cfg_new)
751 {
752   struct DiffHandle diffHandle;
753
754   diffHandle.cfgDiff = GNUNET_CONFIGURATION_create ();
755   diffHandle.cfg_default = cfg_default;
756   GNUNET_CONFIGURATION_iterate (cfg_new, &compare_entries, &diffHandle);
757   return diffHandle.cfgDiff;
758 }
759
760
761 /**
762  * Write only configuration entries that have been changed to configuration file
763  *
764  * @param cfg_default default configuration
765  * @param cfg_new new configuration
766  * @param filename where to write the configuration diff between default and new
767  * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
768  */
769 int
770 GNUNET_CONFIGURATION_write_diffs (const struct GNUNET_CONFIGURATION_Handle
771                                   *cfg_default,
772                                   const struct GNUNET_CONFIGURATION_Handle
773                                   *cfg_new, const char *filename)
774 {
775   int ret;
776   struct GNUNET_CONFIGURATION_Handle *diff;
777
778   diff = GNUNET_CONFIGURATION_get_diff (cfg_default, cfg_new);
779   ret = GNUNET_CONFIGURATION_write (diff, filename);
780   GNUNET_CONFIGURATION_destroy (diff);
781   return ret;
782 }
783
784
785 /**
786  * Set a configuration value that should be a string.
787  *
788  * @param cfg configuration to update
789  * @param section section of interest
790  * @param option option of interest
791  * @param value value to set
792  */
793 void
794 GNUNET_CONFIGURATION_set_value_string (struct GNUNET_CONFIGURATION_Handle *cfg,
795                                        const char *section, const char *option,
796                                        const char *value)
797 {
798   struct ConfigSection *sec;
799   struct ConfigEntry *e;
800   char *nv;
801
802   e = find_entry (cfg, section, option);
803   if (NULL != e)
804   {
805     if (NULL == value)
806     {
807       GNUNET_free_non_null (e->val);
808       e->val = NULL;
809     }
810     else
811     {
812       nv = GNUNET_strdup (value);
813       GNUNET_free_non_null (e->val);
814       e->val = nv;
815     }
816     return;
817   }
818   sec = find_section (cfg, section);
819   if (sec == NULL)
820   {
821     sec = GNUNET_new (struct ConfigSection);
822     sec->name = GNUNET_strdup (section);
823     sec->next = cfg->sections;
824     cfg->sections = sec;
825   }
826   e = GNUNET_new (struct ConfigEntry);
827   e->key = GNUNET_strdup (option);
828   e->val = GNUNET_strdup (value);
829   e->next = sec->entries;
830   sec->entries = e;
831 }
832
833
834 /**
835  * Set a configuration value that should be a number.
836  *
837  * @param cfg configuration to update
838  * @param section section of interest
839  * @param option option of interest
840  * @param number value to set
841  */
842 void
843 GNUNET_CONFIGURATION_set_value_number (struct GNUNET_CONFIGURATION_Handle *cfg,
844                                        const char *section, const char *option,
845                                        unsigned long long number)
846 {
847   char s[64];
848
849   GNUNET_snprintf (s, 64, "%llu", number);
850   GNUNET_CONFIGURATION_set_value_string (cfg, section, option, s);
851 }
852
853
854 /**
855  * Get a configuration value that should be a number.
856  *
857  * @param cfg configuration to inspect
858  * @param section section of interest
859  * @param option option of interest
860  * @param number where to store the numeric value of the option
861  * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
862  */
863 int
864 GNUNET_CONFIGURATION_get_value_number (const struct GNUNET_CONFIGURATION_Handle
865                                        *cfg, const char *section,
866                                        const char *option,
867                                        unsigned long long *number)
868 {
869   struct ConfigEntry *e;
870
871   if (NULL == (e = find_entry (cfg, section, option)))
872     return GNUNET_SYSERR;
873   if (NULL == e->val)
874     return GNUNET_SYSERR;
875   if (1 != SSCANF (e->val, "%llu", number))
876     return GNUNET_SYSERR;
877   return GNUNET_OK;
878 }
879
880 /**
881  * Get a configuration value that should be a floating point number.
882  *
883  * @param cfg configuration to inspect
884  * @param section section of interest
885  * @param option option of interest
886  * @param number where to store the floating value of the option
887  * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
888  */
889 int
890 GNUNET_CONFIGURATION_get_value_float  (const struct GNUNET_CONFIGURATION_Handle
891                                        *cfg, const char *section,
892                                        const char *option,
893                                        float *number)
894 {
895   struct ConfigEntry *e;
896
897   if (NULL == (e = find_entry (cfg, section, option)))
898     return GNUNET_SYSERR;
899   if (NULL == e->val)
900     return GNUNET_SYSERR;
901   if (1 != SSCANF (e->val, "%f", number))
902     return GNUNET_SYSERR;
903   return GNUNET_OK;
904 }
905
906
907
908 /**
909  * Get a configuration value that should be a relative time.
910  *
911  * @param cfg configuration to inspect
912  * @param section section of interest
913  * @param option option of interest
914  * @param time set to the time value stored in the configuration
915  * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
916  */
917 int
918 GNUNET_CONFIGURATION_get_value_time (const struct GNUNET_CONFIGURATION_Handle *cfg,
919                                      const char *section,
920                                      const char *option,
921                                      struct GNUNET_TIME_Relative *time)
922 {
923   struct ConfigEntry *e;
924
925   if (NULL == (e = find_entry (cfg, section, option)))
926     return GNUNET_SYSERR;
927   if (NULL == e->val)
928     return GNUNET_SYSERR;
929   return GNUNET_STRINGS_fancy_time_to_relative (e->val, time);
930 }
931
932
933 /**
934  * Get a configuration value that should be a size in bytes.
935  *
936  * @param cfg configuration to inspect
937  * @param section section of interest
938  * @param option option of interest
939  * @param size set to the size in bytes as stored in the configuration
940  * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
941  */
942 int
943 GNUNET_CONFIGURATION_get_value_size (const struct GNUNET_CONFIGURATION_Handle *cfg,
944                                      const char *section,
945                                      const char *option,
946                                      unsigned long long *size)
947 {
948   struct ConfigEntry *e;
949
950   if (NULL == (e = find_entry (cfg, section, option)))
951     return GNUNET_SYSERR;
952   if (NULL == e->val)
953     return GNUNET_SYSERR;
954   return GNUNET_STRINGS_fancy_size_to_bytes (e->val, size);
955 }
956
957
958 /**
959  * Get a configuration value that should be a string.
960  *
961  * @param cfg configuration to inspect
962  * @param section section of interest
963  * @param option option of interest
964  * @param value will be set to a freshly allocated configuration
965  *        value, or NULL if option is not specified
966  * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
967  */
968 int
969 GNUNET_CONFIGURATION_get_value_string (const struct GNUNET_CONFIGURATION_Handle *cfg,
970                                        const char *section,
971                                        const char *option,
972                                        char **value)
973 {
974   struct ConfigEntry *e;
975
976   if ( (NULL == (e = find_entry (cfg, section, option))) ||
977        (NULL == e->val) )
978   {
979     *value = NULL;
980     return GNUNET_SYSERR;
981   }
982   *value = GNUNET_strdup (e->val);
983   return GNUNET_OK;
984 }
985
986
987 /**
988  * Get a configuration value that should be in a set of
989  * predefined strings
990  *
991  * @param cfg configuration to inspect
992  * @param section section of interest
993  * @param option option of interest
994  * @param choices NULL-terminated list of legal values
995  * @param value will be set to an entry in the legal list,
996  *        or NULL if option is not specified and no default given
997  * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
998  */
999 int
1000 GNUNET_CONFIGURATION_get_value_choice (const struct GNUNET_CONFIGURATION_Handle *cfg,
1001                                        const char *section,
1002                                        const char *option,
1003                                        const char *const *choices,
1004                                        const char **value)
1005 {
1006   struct ConfigEntry *e;
1007   unsigned int i;
1008
1009   if (NULL == (e = find_entry (cfg, section, option)))
1010     return GNUNET_SYSERR;
1011   for (i = 0; NULL != choices[i]; i++)
1012     if (0 == strcasecmp (choices[i], e->val))
1013       break;
1014   if (NULL == choices[i])
1015   {
1016     LOG (GNUNET_ERROR_TYPE_ERROR,
1017          _("Configuration value '%s' for '%s'"
1018            " in section '%s' is not in set of legal choices\n"),
1019          e->val,
1020          option,
1021          section);
1022     return GNUNET_SYSERR;
1023   }
1024   *value = choices[i];
1025   return GNUNET_OK;
1026 }
1027
1028
1029 /**
1030  * Get crockford32-encoded fixed-size binary data from a configuration.
1031  *
1032  * @param cfg configuration to access
1033  * @param section section to access
1034  * @param option option to access
1035  * @param buf where to store the decoded binary result
1036  * @param buf_size exact number of bytes to store in @a buf
1037  * @return #GNUNET_OK on success
1038  *         #GNUNET_NO is the value does not exist
1039  *         #GNUNET_SYSERR on decoding error
1040  */
1041 int
1042 GNUNET_CONFIGURATION_get_data (const struct GNUNET_CONFIGURATION_Handle *cfg,
1043                                const char *section,
1044                                const char *option,
1045                                void *buf,
1046                                size_t buf_size)
1047 {
1048   char *enc;
1049   int res;
1050   size_t data_size;
1051
1052   if (GNUNET_OK !=
1053       (res = GNUNET_CONFIGURATION_get_value_string (cfg,
1054                                                     section,
1055                                                     option,
1056                                                     &enc)))
1057     return res;
1058   data_size = (strlen (enc) * 5) / 8;
1059   if (data_size != buf_size)
1060   {
1061     GNUNET_free (enc);
1062     return GNUNET_SYSERR;
1063   }
1064   if (GNUNET_OK !=
1065       GNUNET_STRINGS_string_to_data (enc,
1066                                      strlen (enc),
1067                                      buf, buf_size))
1068   {
1069     GNUNET_free (enc);
1070     return GNUNET_SYSERR;
1071   }
1072   GNUNET_free (enc);
1073   return GNUNET_OK;
1074 }
1075
1076
1077 /**
1078  * Test if we have a value for a particular option
1079  *
1080  * @param cfg configuration to inspect
1081  * @param section section of interest
1082  * @param option option of interest
1083  * @return #GNUNET_YES if so, #GNUNET_NO if not.
1084  */
1085 int
1086 GNUNET_CONFIGURATION_have_value (const struct GNUNET_CONFIGURATION_Handle *cfg,
1087                                  const char *section,
1088                                  const char *option)
1089 {
1090   struct ConfigEntry *e;
1091
1092   if ((NULL == (e = find_entry (cfg, section, option))) || (NULL == e->val))
1093     return GNUNET_NO;
1094   return GNUNET_YES;
1095 }
1096
1097
1098 /**
1099  * Expand an expression of the form "$FOO/BAR" to "DIRECTORY/BAR"
1100  * where either in the "PATHS" section or the environtment "FOO" is
1101  * set to "DIRECTORY".  We also support default expansion,
1102  * i.e. ${VARIABLE:-default} will expand to $VARIABLE if VARIABLE is
1103  * set in PATHS or the environment, and otherwise to "default".  Note
1104  * that "default" itself can also be a $-expression, thus
1105  * "${VAR1:-{$VAR2}}" will expand to VAR1 and if that is not defined
1106  * to VAR2.
1107  *
1108  * @param cfg configuration to use for path expansion
1109  * @param orig string to $-expand (will be freed!)
1110  * @param depth recursion depth, used to detect recursive expansions
1111  * @return $-expanded string
1112  */
1113 static char *
1114 expand_dollar (const struct GNUNET_CONFIGURATION_Handle *cfg,
1115                char *orig,
1116                unsigned int depth)
1117 {
1118   int i;
1119   char *prefix;
1120   char *result;
1121   char *start;
1122   const char *post;
1123   const char *env;
1124   char *def;
1125   char *end;
1126   unsigned int lopen;
1127   char erased_char;
1128   char *erased_pos;
1129   size_t len;
1130
1131   if (NULL == orig)
1132     return NULL;
1133   if (depth > 128)
1134   {
1135     LOG (GNUNET_ERROR_TYPE_WARNING,
1136          _("Recursive expansion suspected, aborting $-expansion for term `%s'\n"),
1137          orig);
1138     return orig;
1139   }
1140   LOG (GNUNET_ERROR_TYPE_DEBUG,
1141        "Asked to $-expand %s\n",
1142        orig);
1143   if ('$' != orig[0])
1144   {
1145     LOG (GNUNET_ERROR_TYPE_DEBUG,
1146          "Doesn't start with $ - not expanding\n");
1147     return orig;
1148   }
1149   erased_char = 0;
1150   erased_pos = NULL;
1151   if ('{' == orig[1])
1152   {
1153     start = &orig[2];
1154     lopen = 1;
1155     end = &orig[1];
1156     while (lopen > 0)
1157     {
1158       end++;
1159       switch (*end)
1160       {
1161       case '}':
1162         lopen--;
1163         break;
1164       case '{':
1165         lopen++;
1166         break;
1167       case '\0':
1168         LOG (GNUNET_ERROR_TYPE_WARNING,
1169              _("Missing closing `%s' in option `%s'\n"),
1170              "}",
1171              orig);
1172         return orig;
1173       default:
1174         break;
1175       }
1176     }
1177     erased_char = *end;
1178     erased_pos = end;
1179     *end = '\0';
1180     post = end + 1;
1181     def = strchr (orig, ':');
1182     if (NULL != def)
1183     {
1184       *def = '\0';
1185       def++;
1186       if ( ('-' == *def) ||
1187            ('=' == *def) )
1188         def++;
1189       def = GNUNET_strdup (def);
1190     }
1191   }
1192   else
1193   {
1194     start = &orig[1];
1195     def = NULL;
1196     i = 0;
1197     while ( (orig[i] != '/') &&
1198             (orig[i] != '\\') &&
1199             (orig[i] != '\0')  &&
1200             (orig[i] != ' ') )
1201       i++;
1202     if (orig[i] == '\0')
1203     {
1204       post = "";
1205     }
1206     else
1207     {
1208       erased_char = orig[i];
1209       erased_pos = &orig[i];
1210       orig[i] = '\0';
1211       post = &orig[i + 1];
1212     }
1213   }
1214   LOG (GNUNET_ERROR_TYPE_DEBUG,
1215        "Split into `%s' and `%s' with default %s\n",
1216        start,
1217        post,
1218        def);
1219   if (GNUNET_OK !=
1220       GNUNET_CONFIGURATION_get_value_string (cfg,
1221                                              "PATHS",
1222                                              start,
1223                                              &prefix))
1224   {
1225     if (NULL == (env = getenv (start)))
1226     {
1227       /* try default */
1228       def = expand_dollar (cfg, def, depth + 1);
1229       env = def;
1230     }
1231     if (NULL == env)
1232     {
1233       start = GNUNET_strdup (start);
1234       if (erased_pos)
1235         *erased_pos = erased_char;
1236       LOG (GNUNET_ERROR_TYPE_WARNING,
1237            _("Failed to expand `%s' in `%s' as it is neither found in [PATHS] nor defined as an environmental variable\n"),
1238            start, orig);
1239       GNUNET_free (start);
1240       return orig;
1241     }
1242     prefix = GNUNET_strdup (env);
1243   }
1244   prefix = GNUNET_CONFIGURATION_expand_dollar (cfg, prefix);
1245   if ( (erased_pos) && ('}' != erased_char) )
1246   {
1247     len = strlen (prefix) + 1;
1248     prefix = GNUNET_realloc (prefix, len + 1);
1249     prefix[len - 1] = erased_char;
1250     prefix[len] = '\0';
1251   }
1252   result = GNUNET_malloc (strlen (prefix) + strlen (post) + 1);
1253   strcpy (result, prefix);
1254   strcat (result, post);
1255   GNUNET_free_non_null (def);
1256   GNUNET_free (prefix);
1257   GNUNET_free (orig);
1258   return result;
1259 }
1260
1261
1262 /**
1263  * Expand an expression of the form "$FOO/BAR" to "DIRECTORY/BAR"
1264  * where either in the "PATHS" section or the environtment "FOO" is
1265  * set to "DIRECTORY".  We also support default expansion,
1266  * i.e. ${VARIABLE:-default} will expand to $VARIABLE if VARIABLE is
1267  * set in PATHS or the environment, and otherwise to "default".  Note
1268  * that "default" itself can also be a $-expression, thus
1269  * "${VAR1:-{$VAR2}}" will expand to VAR1 and if that is not defined
1270  * to VAR2.
1271  *
1272  * @param cfg configuration to use for path expansion
1273  * @param orig string to $-expand (will be freed!).  Note that multiple
1274  *          $-expressions can be present in this string.  They will all be
1275  *          $-expanded.
1276  * @return $-expanded string
1277  */
1278 char *
1279 GNUNET_CONFIGURATION_expand_dollar (const struct GNUNET_CONFIGURATION_Handle *cfg,
1280                                     char *orig)
1281 {
1282   char *dup;
1283   size_t i;
1284   size_t len;
1285
1286   for (i = 0; '\0' != orig[i]; i++)
1287   {
1288     if ('$' != orig[i])
1289       continue;
1290     dup = GNUNET_strdup (orig + i);
1291     dup = expand_dollar (cfg, dup, 0);
1292     len = strlen (dup) + 1;
1293     orig = GNUNET_realloc (orig, i + len);
1294     GNUNET_memcpy (orig + i, dup, len);
1295     GNUNET_free (dup);
1296   }
1297   return orig;
1298 }
1299
1300
1301 /**
1302  * Get a configuration value that should be a string.
1303  *
1304  * @param cfg configuration to inspect
1305  * @param section section of interest
1306  * @param option option of interest
1307  * @param value will be set to a freshly allocated configuration
1308  *        value, or NULL if option is not specified
1309  * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
1310  */
1311 int
1312 GNUNET_CONFIGURATION_get_value_filename (const struct GNUNET_CONFIGURATION_Handle *cfg,
1313                                          const char *section,
1314                                          const char *option,
1315                                          char **value)
1316 {
1317   char *tmp;
1318
1319   if (GNUNET_OK !=
1320       GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &tmp))
1321   {
1322     LOG (GNUNET_ERROR_TYPE_DEBUG,
1323          "Failed to retrieve filename\n");
1324     *value = NULL;
1325     return GNUNET_SYSERR;
1326   }
1327   tmp = GNUNET_CONFIGURATION_expand_dollar (cfg, tmp);
1328   *value = GNUNET_STRINGS_filename_expand (tmp);
1329   GNUNET_free (tmp);
1330   if (*value == NULL)
1331     return GNUNET_SYSERR;
1332   return GNUNET_OK;
1333 }
1334
1335
1336 /**
1337  * Get a configuration value that should be in a set of
1338  * "YES" or "NO".
1339  *
1340  * @param cfg configuration to inspect
1341  * @param section section of interest
1342  * @param option option of interest
1343  * @return #GNUNET_YES, #GNUNET_NO or #GNUNET_SYSERR
1344  */
1345 int
1346 GNUNET_CONFIGURATION_get_value_yesno (const struct GNUNET_CONFIGURATION_Handle *cfg,
1347                                       const char *section,
1348                                       const char *option)
1349 {
1350   static const char *yesno[] = { "YES", "NO", NULL };
1351   const char *val;
1352   int ret;
1353
1354   ret =
1355       GNUNET_CONFIGURATION_get_value_choice (cfg, section, option, yesno, &val);
1356   if (ret == GNUNET_SYSERR)
1357     return ret;
1358   if (val == yesno[0])
1359     return GNUNET_YES;
1360   return GNUNET_NO;
1361 }
1362
1363
1364 /**
1365  * Iterate over the set of filenames stored in a configuration value.
1366  *
1367  * @param cfg configuration to inspect
1368  * @param section section of interest
1369  * @param option option of interest
1370  * @param cb function to call on each filename
1371  * @param cb_cls closure for @a cb
1372  * @return number of filenames iterated over, -1 on error
1373  */
1374 int
1375 GNUNET_CONFIGURATION_iterate_value_filenames (const struct GNUNET_CONFIGURATION_Handle *cfg,
1376                                               const char *section,
1377                                               const char *option,
1378                                               GNUNET_FileNameCallback cb,
1379                                               void *cb_cls)
1380 {
1381   char *list;
1382   char *pos;
1383   char *end;
1384   char old;
1385   int ret;
1386
1387   if (GNUNET_OK !=
1388       GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &list))
1389     return 0;
1390   GNUNET_assert (list != NULL);
1391   ret = 0;
1392   pos = list;
1393   while (1)
1394   {
1395     while (pos[0] == ' ')
1396       pos++;
1397     if (strlen (pos) == 0)
1398       break;
1399     end = pos + 1;
1400     while ((end[0] != ' ') && (end[0] != '\0'))
1401     {
1402       if (end[0] == '\\')
1403       {
1404         switch (end[1])
1405         {
1406         case '\\':
1407         case ' ':
1408           memmove (end, &end[1], strlen (&end[1]) + 1);
1409         case '\0':
1410           /* illegal, but just keep it */
1411           break;
1412         default:
1413           /* illegal, but just ignore that there was a '/' */
1414           break;
1415         }
1416       }
1417       end++;
1418     }
1419     old = end[0];
1420     end[0] = '\0';
1421     if (strlen (pos) > 0)
1422     {
1423       ret++;
1424       if ((cb != NULL) && (GNUNET_OK != cb (cb_cls, pos)))
1425       {
1426         ret = GNUNET_SYSERR;
1427         break;
1428       }
1429     }
1430     if (old == '\0')
1431       break;
1432     pos = end + 1;
1433   }
1434   GNUNET_free (list);
1435   return ret;
1436 }
1437
1438
1439 /**
1440  * FIXME.
1441  *
1442  * @param value FIXME
1443  * @return FIXME
1444  */
1445 static char *
1446 escape_name (const char *value)
1447 {
1448   char *escaped;
1449   const char *rpos;
1450   char *wpos;
1451
1452   escaped = GNUNET_malloc (strlen (value) * 2 + 1);
1453   memset (escaped, 0, strlen (value) * 2 + 1);
1454   rpos = value;
1455   wpos = escaped;
1456   while (rpos[0] != '\0')
1457   {
1458     switch (rpos[0])
1459     {
1460     case '\\':
1461     case ' ':
1462       wpos[0] = '\\';
1463       wpos[1] = rpos[0];
1464       wpos += 2;
1465       break;
1466     default:
1467       wpos[0] = rpos[0];
1468       wpos++;
1469     }
1470     rpos++;
1471   }
1472   return escaped;
1473 }
1474
1475
1476 /**
1477  * FIXME.
1478  *
1479  * @param cls string we compare with (const char*)
1480  * @param fn filename we are currently looking at
1481  * @return #GNUNET_OK if the names do not match, #GNUNET_SYSERR if they do
1482  */
1483 static int
1484 test_match (void *cls, const char *fn)
1485 {
1486   const char *of = cls;
1487
1488   return (0 == strcmp (of, fn)) ? GNUNET_SYSERR : GNUNET_OK;
1489 }
1490
1491
1492 /**
1493  * Append a filename to a configuration value that
1494  * represents a list of filenames
1495  *
1496  * @param cfg configuration to update
1497  * @param section section of interest
1498  * @param option option of interest
1499  * @param value filename to append
1500  * @return #GNUNET_OK on success,
1501  *         #GNUNET_NO if the filename already in the list
1502  *         #GNUNET_SYSERR on error
1503  */
1504 int
1505 GNUNET_CONFIGURATION_append_value_filename (struct GNUNET_CONFIGURATION_Handle *cfg,
1506                                             const char *section,
1507                                             const char *option,
1508                                             const char *value)
1509 {
1510   char *escaped;
1511   char *old;
1512   char *nw;
1513
1514   if (GNUNET_SYSERR ==
1515       GNUNET_CONFIGURATION_iterate_value_filenames (cfg, section, option,
1516                                                     &test_match,
1517                                                     (void *) value))
1518     return GNUNET_NO;           /* already exists */
1519   if (GNUNET_OK !=
1520       GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &old))
1521     old = GNUNET_strdup ("");
1522   escaped = escape_name (value);
1523   nw = GNUNET_malloc (strlen (old) + strlen (escaped) + 2);
1524   strcpy (nw, old);
1525   if (strlen (old) > 0)
1526     strcat (nw, " ");
1527   strcat (nw, escaped);
1528   GNUNET_CONFIGURATION_set_value_string (cfg, section, option, nw);
1529   GNUNET_free (old);
1530   GNUNET_free (nw);
1531   GNUNET_free (escaped);
1532   return GNUNET_OK;
1533 }
1534
1535
1536 /**
1537  * Remove a filename from a configuration value that
1538  * represents a list of filenames
1539  *
1540  * @param cfg configuration to update
1541  * @param section section of interest
1542  * @param option option of interest
1543  * @param value filename to remove
1544  * @return #GNUNET_OK on success,
1545  *         #GNUNET_NO if the filename is not in the list,
1546  *         #GNUNET_SYSERR on error
1547  */
1548 int
1549 GNUNET_CONFIGURATION_remove_value_filename (struct GNUNET_CONFIGURATION_Handle
1550                                             *cfg, const char *section,
1551                                             const char *option,
1552                                             const char *value)
1553 {
1554   char *list;
1555   char *pos;
1556   char *end;
1557   char *match;
1558   char old;
1559
1560   if (GNUNET_OK !=
1561       GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &list))
1562     return GNUNET_NO;
1563   match = escape_name (value);
1564   pos = list;
1565   while (1)
1566   {
1567     while (pos[0] == ' ')
1568       pos++;
1569     if (strlen (pos) == 0)
1570       break;
1571     end = pos + 1;
1572     while ((end[0] != ' ') && (end[0] != '\0'))
1573     {
1574       if (end[0] == '\\')
1575       {
1576         switch (end[1])
1577         {
1578         case '\\':
1579         case ' ':
1580           end++;
1581           break;
1582         case '\0':
1583           /* illegal, but just keep it */
1584           break;
1585         default:
1586           /* illegal, but just ignore that there was a '/' */
1587           break;
1588         }
1589       }
1590       end++;
1591     }
1592     old = end[0];
1593     end[0] = '\0';
1594     if (0 == strcmp (pos, match))
1595     {
1596       if (old != '\0')
1597         memmove (pos, &end[1], strlen (&end[1]) + 1);
1598       else
1599       {
1600         if (pos != list)
1601           pos[-1] = '\0';
1602         else
1603           pos[0] = '\0';
1604       }
1605       GNUNET_CONFIGURATION_set_value_string (cfg, section, option, list);
1606       GNUNET_free (list);
1607       GNUNET_free (match);
1608       return GNUNET_OK;
1609     }
1610     if (old == '\0')
1611       break;
1612     end[0] = old;
1613     pos = end + 1;
1614   }
1615   GNUNET_free (list);
1616   GNUNET_free (match);
1617   return GNUNET_NO;
1618 }
1619
1620
1621 /**
1622  * Wrapper around #GNUNET_CONFIGURATION_parse.  Called on each
1623  * file in a directory, we trigger parsing on those files that
1624  * end with ".conf".
1625  *
1626  * @param cls the cfg
1627  * @param filename file to parse
1628  * @return #GNUNET_OK on success
1629  */
1630 static int
1631 parse_configuration_file (void *cls, const char *filename)
1632 {
1633   struct GNUNET_CONFIGURATION_Handle *cfg = cls;
1634   char * ext;
1635   int ret;
1636
1637   /* Examine file extension */
1638   ext = strrchr (filename, '.');
1639   if ((NULL == ext) || (0 != strcmp (ext, ".conf")))
1640   {
1641     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1642                 "Skipping file `%s'\n",
1643                 filename);
1644     return GNUNET_OK;
1645   }
1646
1647   ret = GNUNET_CONFIGURATION_parse (cfg, filename);
1648   return ret;
1649 }
1650
1651
1652 /**
1653  * Load default configuration.  This function will parse the
1654  * defaults from the given defaults_d directory.
1655  *
1656  * @param cfg configuration to update
1657  * @param defaults_d directory with the defaults
1658  * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
1659  */
1660 int
1661 GNUNET_CONFIGURATION_load_from (struct GNUNET_CONFIGURATION_Handle *cfg,
1662                                 const char *defaults_d)
1663 {
1664   if (GNUNET_SYSERR ==
1665       GNUNET_DISK_directory_scan (defaults_d, &parse_configuration_file, cfg))
1666     return GNUNET_SYSERR;       /* no configuration at all found */
1667   return GNUNET_OK;
1668 }
1669
1670
1671 /* end of configuration.c */