fixing bio testcase and a bug in bio.c, also indenting
[oweals/gnunet.git] / src / util / configuration.c
1 /*
2      This file is part of GNUnet.
3      (C) 2006, 2007, 2008, 2009 Christian Grothoff (and other contributing authors)
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 2, 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., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20
21 /**
22  * @file src/util/configuration.c
23  * @brief configuration management
24  *
25  * @author Christian Grothoff
26  */
27
28 #include "platform.h"
29 #include "gnunet_common.h"
30 #include "gnunet_configuration_lib.h"
31 #include "gnunet_crypto_lib.h"
32 #include "gnunet_disk_lib.h"
33 #include "gnunet_os_lib.h"
34 #include "gnunet_strings_lib.h"
35
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  * Create a GNUNET_CONFIGURATION_Handle.
104  *
105  * @return fresh configuration object
106  */
107 struct GNUNET_CONFIGURATION_Handle *
108 GNUNET_CONFIGURATION_create ()
109 {
110   return GNUNET_malloc (sizeof (struct GNUNET_CONFIGURATION_Handle));
111 }
112
113
114 /**
115  * Destroy configuration object.
116  *
117  * @param cfg configuration to destroy
118  */
119 void
120 GNUNET_CONFIGURATION_destroy (struct GNUNET_CONFIGURATION_Handle *cfg)
121 {
122   struct ConfigSection *sec;
123   struct ConfigEntry *ent;
124
125   while (NULL != (sec = cfg->sections))
126     {
127       cfg->sections = sec->next;
128       while (NULL != (ent = sec->entries))
129         {
130           sec->entries = ent->next;
131           GNUNET_free (ent->key);
132           GNUNET_free_non_null (ent->val);
133           GNUNET_free (ent);
134         }
135       GNUNET_free (sec->name);
136       GNUNET_free (sec);
137     }
138   GNUNET_free (cfg);
139 }
140
141
142 /**
143  * Parse a configuration file, add all of the options in the
144  * file to the configuration environment.
145  *
146  * @param cfg configuration to update
147  * @param filename name of the configuration file
148  * @return GNUNET_OK on success, GNUNET_SYSERR on error
149  */
150 int
151 GNUNET_CONFIGURATION_parse (struct GNUNET_CONFIGURATION_Handle *cfg,
152                             const char *filename)
153 {
154   int dirty;
155   char line[256];
156   char tag[64];
157   char value[192];
158   FILE *fp;
159   unsigned int nr;
160   int i;
161   int emptyline;
162   int ret;
163   char *section;
164   char *fn;
165
166   fn = GNUNET_STRINGS_filename_expand (filename);
167   dirty = cfg->dirty;           /* back up value! */
168   if (NULL == (fp = FOPEN (fn, "r")))
169     {
170       GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "fopen", fn);
171       GNUNET_free (fn);
172       return GNUNET_SYSERR;
173     }
174   GNUNET_free (fn);
175   ret = GNUNET_OK;
176   section = GNUNET_strdup ("");
177   memset (line, 0, 256);
178   nr = 0;
179   while (NULL != fgets (line, 255, fp))
180     {
181       nr++;
182       for (i = 0; i < 255; i++)
183         if (line[i] == '\t')
184           line[i] = ' ';
185       if (line[0] == '\n' || line[0] == '#' || line[0] == '%' ||
186           line[0] == '\r')
187         continue;
188       emptyline = 1;
189       for (i = 0; (i < 255 && line[i] != 0); i++)
190         if (line[i] != ' ' && line[i] != '\n' && line[i] != '\r')
191           emptyline = 0;
192       if (emptyline == 1)
193         continue;
194       /* remove tailing whitespace */
195       for (i = strlen (line) - 1; (i >= 0) && (isspace (line[i])); i--)
196         line[i] = '\0';
197       if (1 == sscanf (line, "@INLINE@ %191[^\n]", value))
198         {
199           /* @INLINE@ value */
200           if (0 != GNUNET_CONFIGURATION_parse (cfg, value))
201             ret = GNUNET_SYSERR;        /* failed to parse included config */
202         }
203       else if (1 == sscanf (line, "[%99[^]]]", value))
204         {
205           /* [value] */
206           GNUNET_free (section);
207           section = GNUNET_strdup (value);
208         }
209       else if (2 == sscanf (line, " %63[^= ] = %191[^\n]", tag, value))
210         {
211           /* tag = value */
212           /* Strip LF */
213           i = strlen (value) - 1;
214           while ((i >= 0) && (isspace (value[i])))
215             value[i--] = '\0';
216           /* remove quotes */
217           i = 0;
218           if (value[0] == '"')
219             {
220               i = 1;
221               while ((value[i] != '\0') && (value[i] != '"'))
222                 i++;
223               if (value[i] == '"')
224                 {
225                   value[i] = '\0';
226                   i = 1;
227                 }
228               else
229                 i = 0;
230             }
231           GNUNET_CONFIGURATION_set_value_string (cfg,
232                                                  section, tag, &value[i]);
233         }
234       else if (1 == sscanf (line, " %63[^= ] =[^\n]", tag))
235         {
236           /* tag = */
237           GNUNET_CONFIGURATION_set_value_string (cfg, section, tag, "");
238         }
239       else
240         {
241           /* parse error */
242           GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
243                       _
244                       ("Syntax error in configuration file `%s' at line %u.\n"),
245                       filename, nr);
246           ret = GNUNET_SYSERR;
247           break;
248         }
249     }
250   GNUNET_assert (0 == fclose (fp));
251   /* restore dirty flag - anything we set in the meantime
252      came from disk */
253   cfg->dirty = dirty;
254   GNUNET_free (section);
255   return ret;
256 }
257
258
259 /**
260  * Test if there are configuration options that were
261  * changed since the last save.
262  *
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)
265  */
266 int
267 GNUNET_CONFIGURATION_is_dirty (const struct GNUNET_CONFIGURATION_Handle *cfg)
268 {
269   return cfg->dirty;
270 }
271
272
273 /**
274  * Write configuration file.
275  *
276  * @param cfg configuration to write
277  * @param filename where to write the configuration
278  * @return GNUNET_OK on success, GNUNET_SYSERR on error
279  */
280 int
281 GNUNET_CONFIGURATION_write (struct GNUNET_CONFIGURATION_Handle *cfg,
282                             const char *filename)
283 {
284   struct ConfigSection *sec;
285   struct ConfigEntry *ent;
286   FILE *fp;
287   int error;
288   char *fn;
289   char *val;
290   char *pos;
291
292   fn = GNUNET_STRINGS_filename_expand (filename);
293   GNUNET_DISK_directory_create_for_file (fn);
294   if (NULL == (fp = FOPEN (fn, "w")))
295     {
296       GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "fopen", fn);
297       GNUNET_free (fn);
298       return GNUNET_SYSERR;
299     }
300   GNUNET_free (fn);
301   error = 0;
302   sec = cfg->sections;
303   while (sec != NULL)
304     {
305       if (0 > fprintf (fp, "[%s]\n", sec->name))
306         {
307           error = 1;
308           break;
309         }
310       ent = sec->entries;
311       while (ent != NULL)
312         {
313           if (ent->val != NULL)
314             {
315               val = GNUNET_malloc (strlen (ent->val) * 2 + 1);
316               strcpy (val, ent->val);
317               while (NULL != (pos = strstr (val, "\n")))
318                 {
319                   memmove (&pos[2], &pos[1], strlen (&pos[1]));
320                   pos[0] = '\\';
321                   pos[1] = 'n';
322                 }
323               if (0 > fprintf (fp, "%s = %s\n", ent->key, val))
324                 {
325                   error = 1;
326                   GNUNET_free (val);
327                   break;
328                 }
329               GNUNET_free (val);
330             }
331           ent = ent->next;
332         }
333       if (error != 0)
334         break;
335       if (0 > fprintf (fp, "\n"))
336         {
337           error = 1;
338           break;
339         }
340       sec = sec->next;
341     }
342   if (error != 0)
343     GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "fprintf", filename);
344   GNUNET_assert (0 == fclose (fp));
345   if (error != 0)
346     {
347       cfg->dirty = GNUNET_SYSERR;       /* last write failed */
348       return GNUNET_SYSERR;
349     }
350   cfg->dirty = GNUNET_NO;       /* last write succeeded */
351   return GNUNET_OK;
352 }
353
354
355 /**
356  * Iterate over all options in the configuration.
357  *
358  * @param cfg configuration to inspect
359  * @param iter function to call on each option
360  * @param iter_cls closure for iter
361  */
362 void
363 GNUNET_CONFIGURATION_iterate (const struct GNUNET_CONFIGURATION_Handle *cfg,
364                               GNUNET_CONFIGURATION_Iterator iter,
365                               void *iter_cls)
366 {
367   struct ConfigSection *spos;
368   struct ConfigEntry *epos;
369
370   spos = cfg->sections;
371   while (spos != NULL)
372     {
373       epos = spos->entries;
374       while (epos != NULL)
375         {
376           iter (iter_cls, spos->name, epos->key, epos->val);
377           epos = epos->next;
378         }
379       spos = spos->next;
380     }
381 }
382
383
384 /**
385  * Copy a configuration value to the given target configuration.
386  * Overwrites existing entries.
387  *
388  * @param cls the destination configuration (struct GNUNET_CONFIGURATION_Handle*)
389  * @param section section for the value
390  * @param option option name of the value
391  * @param value value to copy 
392  */
393 static void
394 copy_entry (void *cls,
395             const char *section, const char *option, const char *value)
396 {
397   struct GNUNET_CONFIGURATION_Handle *dst = cls;
398   GNUNET_CONFIGURATION_set_value_string (dst, section, option, value);
399 }
400
401
402 /**
403  * Duplicate an existing configuration object.
404  *
405  * @param cfg configuration to duplicate
406  * @return duplicate configuration
407  */
408 struct GNUNET_CONFIGURATION_Handle *
409 GNUNET_CONFIGURATION_dup (const struct GNUNET_CONFIGURATION_Handle *cfg)
410 {
411   struct GNUNET_CONFIGURATION_Handle *ret;
412
413   ret = GNUNET_CONFIGURATION_create ();
414   GNUNET_CONFIGURATION_iterate (cfg, &copy_entry, ret);
415   return ret;
416 }
417
418
419 /**
420  * FIXME.
421  *
422  * @param cfg FIXME
423  * @param section FIXME
424  * @return matching entry, NULL if not found
425  */
426 static struct ConfigSection *
427 findSection (const struct GNUNET_CONFIGURATION_Handle *cfg,
428              const char *section)
429 {
430   struct ConfigSection *pos;
431
432   pos = cfg->sections;
433   while ((pos != NULL) && (0 != strcasecmp (section, pos->name)))
434     pos = pos->next;
435   return pos;
436 }
437
438
439 /**
440  * FIXME.
441  *
442  * @param cfg FIXME
443  * @param section FIXME
444  * @param key FIXME
445  * @return matching entry, NULL if not found
446  */
447 static struct ConfigEntry *
448 findEntry (const struct GNUNET_CONFIGURATION_Handle *cfg,
449            const char *section, const char *key)
450 {
451   struct ConfigSection *sec;
452   struct ConfigEntry *pos;
453
454   sec = findSection (cfg, section);
455   if (sec == NULL)
456     return NULL;
457   pos = sec->entries;
458   while ((pos != NULL) && (0 != strcasecmp (key, pos->key)))
459     pos = pos->next;
460   return pos;
461 }
462
463
464 /**
465  * Set a configuration value that should be a string.
466  *
467  * @param cfg configuration to update
468  * @param section section of interest
469  * @param option option of interest
470  * @param value value to set
471  */
472 void
473 GNUNET_CONFIGURATION_set_value_string (struct GNUNET_CONFIGURATION_Handle
474                                        *cfg,
475                                        const char *section,
476                                        const char *option, const char *value)
477 {
478   struct ConfigSection *sec;
479   struct ConfigEntry *e;
480
481   e = findEntry (cfg, section, option);
482   if (e != NULL)
483     {
484       GNUNET_free_non_null (e->val);
485       e->val = GNUNET_strdup (value);
486       return;
487     }
488   sec = findSection (cfg, section);
489   if (sec == NULL)
490     {
491       sec = GNUNET_malloc (sizeof (struct ConfigSection));
492       sec->name = GNUNET_strdup (section);
493       sec->next = cfg->sections;
494       cfg->sections = sec;
495     }
496   e = GNUNET_malloc (sizeof (struct ConfigEntry));
497   e->key = GNUNET_strdup (option);
498   e->val = GNUNET_strdup (value);
499   e->next = sec->entries;
500   sec->entries = e;
501 }
502
503
504 /**
505  * Set a configuration value that should be a number.
506  *
507  * @param cfg configuration to update
508  * @param section section of interest
509  * @param option option of interest
510  * @param number value to set
511  */
512 void
513 GNUNET_CONFIGURATION_set_value_number (struct GNUNET_CONFIGURATION_Handle
514                                        *cfg, const char *section,
515                                        const char *option,
516                                        unsigned long long number)
517 {
518   char s[64];
519   GNUNET_snprintf (s, 64, "%llu", number);
520   GNUNET_CONFIGURATION_set_value_string (cfg, section, option, s);
521 }
522
523
524 /**
525  * Get a configuration value that should be a number.
526  *
527  * @param cfg configuration to inspect
528  * @param section section of interest
529  * @param option option of interest
530  * @param number where to store the numeric value of the option
531  * @return GNUNET_OK on success, GNUNET_SYSERR on error
532  */
533 int
534 GNUNET_CONFIGURATION_get_value_number (const struct
535                                        GNUNET_CONFIGURATION_Handle *cfg,
536                                        const char *section,
537                                        const char *option,
538                                        unsigned long long *number)
539 {
540   struct ConfigEntry *e;
541
542   e = findEntry (cfg, section, option);
543   if (e == NULL)
544     return GNUNET_SYSERR;
545   if (1 != SSCANF (e->val, "%llu", number))
546     return GNUNET_SYSERR;
547   return GNUNET_OK;
548 }
549
550
551 /**
552  * Get a configuration value that should be a relative time.
553  *
554  * @param cfg configuration to inspect
555  * @param section section of interest
556  * @param option option of interest
557  * @param time set to the time value stored in the configuration
558  * @return GNUNET_OK on success, GNUNET_SYSERR on error
559  */
560 int
561 GNUNET_CONFIGURATION_get_value_time (const struct GNUNET_CONFIGURATION_Handle
562                                      *cfg, const char *section,
563                                      const char *option,
564                                      struct GNUNET_TIME_Relative *time)
565 {
566   unsigned long long num;
567   int ret;
568
569   ret = GNUNET_CONFIGURATION_get_value_number (cfg, section, option, &num);
570   if (ret == GNUNET_OK)
571     time->value = (uint64_t) num;
572   return ret;
573 }
574
575
576 /**
577  * Get a configuration value that should be a string.
578  *
579  * @param cfg configuration to inspect
580  * @param section section of interest
581  * @param option option of interest
582  * @param value will be set to a freshly allocated configuration
583  *        value, or NULL if option is not specified
584  * @return GNUNET_OK on success, GNUNET_SYSERR on error
585  */
586 int
587 GNUNET_CONFIGURATION_get_value_string (const struct
588                                        GNUNET_CONFIGURATION_Handle *cfg,
589                                        const char *section,
590                                        const char *option, char **value)
591 {
592   struct ConfigEntry *e;
593
594   e = findEntry (cfg, section, option);
595   if ((e == NULL) || (e->val == NULL))
596     {
597       *value = NULL;
598       return GNUNET_SYSERR;
599     }
600   *value = GNUNET_strdup (e->val);
601   return GNUNET_OK;
602 }
603
604
605 /**
606  * Get a configuration value that should be in a set of
607  * predefined strings
608  *
609  * @param cfg configuration to inspect
610  * @param section section of interest
611  * @param option option of interest
612  * @param choices NULL-terminated list of legal values
613  * @param value will be set to an entry in the legal list,
614  *        or NULL if option is not specified and no default given
615  * @return GNUNET_OK on success, GNUNET_SYSERR on error
616  */
617 int
618 GNUNET_CONFIGURATION_get_value_choice (const struct
619                                        GNUNET_CONFIGURATION_Handle *cfg,
620                                        const char *section,
621                                        const char *option,
622                                        const char **choices,
623                                        const char **value)
624 {
625   struct ConfigEntry *e;
626   int i;
627
628   e = findEntry (cfg, section, option);
629   if (e == NULL)
630     return GNUNET_SYSERR;
631   i = 0;
632   while (choices[i] != NULL)
633     {
634       if (0 == strcasecmp (choices[i], e->val))
635         break;
636       i++;
637     }
638   if (choices[i] == NULL)
639     {
640       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
641                   _("Configuration value '%s' for '%s'"
642                     " in section '%s' is not in set of legal choices\n"),
643                   e->val, option, section);
644       return GNUNET_SYSERR;
645     }
646   *value = choices[i];
647   return GNUNET_OK;
648 }
649
650
651 /**
652  * Test if we have a value for a particular option
653  * @param cfg configuration to inspect
654  * @param section section of interest
655  * @param option option of interest
656  * @return GNUNET_YES if so, GNUNET_NO if not.
657  */
658 int
659 GNUNET_CONFIGURATION_have_value (const struct GNUNET_CONFIGURATION_Handle
660                                  *cfg, const char *section,
661                                  const char *option)
662 {
663   struct ConfigEntry *e;
664   if ((NULL == (e = findEntry (cfg, section, option))) || (e->val == NULL))
665     return GNUNET_NO;
666   return GNUNET_YES;
667 }
668
669
670 /**
671  * Expand an expression of the form "$FOO/BAR" to "DIRECTORY/BAR"
672  * where either in the "PATHS" section or the environtment
673  * "FOO" is set to "DIRECTORY".
674  *
675  * @param cfg configuration to use for path expansion
676  * @param orig string to $-expand (will be freed!)
677  * @return $-expanded string
678  */
679 char *
680 GNUNET_CONFIGURATION_expand_dollar (const struct GNUNET_CONFIGURATION_Handle
681                                     *cfg, char *orig)
682 {
683   int i;
684   char *prefix;
685   char *result;
686   const char *post;
687   const char *env;
688
689   if (orig[0] != '$')
690     return orig;
691   i = 0;
692   while ((orig[i] != '/') && (orig[i] != '\\') && (orig[i] != '\0'))
693     i++;
694   if (orig[i] == '\0')
695     {
696       post = "";
697     }
698   else
699     {
700       orig[i] = '\0';
701       post = &orig[i + 1];
702     }
703   if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (cfg,
704                                                           "PATHS",
705                                                           &orig[1], &prefix))
706     {
707       if (NULL == (env = getenv (&orig[1])))
708         {
709           orig[i] = DIR_SEPARATOR;
710           return orig;
711         }
712       prefix = GNUNET_strdup (env);
713     }
714   result = GNUNET_malloc (strlen (prefix) + strlen (post) + 2);
715   strcpy (result, prefix);
716   if ((strlen (prefix) == 0) ||
717       ((prefix[strlen (prefix) - 1] != DIR_SEPARATOR) && (strlen (post) > 0)))
718     strcat (result, DIR_SEPARATOR_STR);
719   strcat (result, post);
720   GNUNET_free (prefix);
721   GNUNET_free (orig);
722   return result;
723 }
724
725
726 /**
727  * Get a configuration value that should be a string.
728  *
729  * @param cfg configuration to inspect
730  * @param section section of interest
731  * @param option option of interest
732  * @param value will be set to a freshly allocated configuration
733  *        value, or NULL if option is not specified
734  * @return GNUNET_OK on success, GNUNET_SYSERR on error
735  */
736 int
737 GNUNET_CONFIGURATION_get_value_filename (const struct
738                                          GNUNET_CONFIGURATION_Handle *cfg,
739                                          const char *section,
740                                          const char *option, char **value)
741 {
742   int ret;
743   char *tmp;
744
745   tmp = NULL;
746   ret = GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &tmp);
747   if (ret == GNUNET_SYSERR)
748     return ret;
749   if (tmp != NULL)
750     {
751       tmp = GNUNET_CONFIGURATION_expand_dollar (cfg, tmp);
752       *value = GNUNET_STRINGS_filename_expand (tmp);
753       GNUNET_free (tmp);
754       if (*value == NULL)
755         ret = GNUNET_SYSERR;
756     }
757   else
758     {
759       *value = NULL;
760     }
761   return ret;
762 }
763
764
765 /**
766  * Get a configuration value that should be in a set of
767  * "GNUNET_YES" or "GNUNET_NO".
768  *
769  * @param cfg configuration to inspect
770  * @param section section of interest
771  * @param option option of interest
772  * @return GNUNET_YES, GNUNET_NO or GNUNET_SYSERR
773  */
774 int
775 GNUNET_CONFIGURATION_get_value_yesno (const struct GNUNET_CONFIGURATION_Handle
776                                       *cfg, const char *section,
777                                       const char *option)
778 {
779   static const char *yesno[] = { "YES", "NO", NULL };
780   const char *val;
781   int ret;
782
783   ret = GNUNET_CONFIGURATION_get_value_choice (cfg,
784                                                section, option, yesno, &val);
785   if (ret == GNUNET_SYSERR)
786     return ret;
787   if (val == yesno[0])
788     return GNUNET_YES;
789   return GNUNET_NO;
790 }
791
792
793 /**
794  * Iterate over the set of filenames stored in a configuration value.
795  *
796  * @param cfg configuration to inspect
797  * @param section section of interest
798  * @param option option of interest
799  * @param cb function to call on each filename
800  * @param cb_cls closure for cb
801  * @return number of filenames iterated over, -1 on error
802  */
803 int
804 GNUNET_CONFIGURATION_iterate_value_filenames (const struct
805                                               GNUNET_CONFIGURATION_Handle
806                                               *cfg, const char *section,
807                                               const char *option,
808                                               GNUNET_FileNameCallback cb,
809                                               void *cb_cls)
810 {
811   char *list;
812   char *pos;
813   char *end;
814   char old;
815   int ret;
816
817   if (GNUNET_OK !=
818       GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &list))
819     return 0;
820   GNUNET_assert (list != NULL);
821   ret = 0;
822   pos = list;
823   while (1)
824     {
825       while (pos[0] == ' ')
826         pos++;
827       if (strlen (pos) == 0)
828         break;
829       end = pos + 1;
830       while ((end[0] != ' ') && (end[0] != '\0'))
831         {
832           if (end[0] == '\\')
833             {
834               switch (end[1])
835                 {
836                 case '\\':
837                 case ' ':
838                   memmove (end, &end[1], strlen (&end[1]) + 1);
839                 case '\0':
840                   /* illegal, but just keep it */
841                   break;
842                 default:
843                   /* illegal, but just ignore that there was a '/' */
844                   break;
845                 }
846             }
847           end++;
848         }
849       old = end[0];
850       end[0] = '\0';
851       if (strlen (pos) > 0)
852         {
853           ret++;
854           if ((cb != NULL) && (GNUNET_OK != cb (cb_cls, pos)))
855             {
856               ret = GNUNET_SYSERR;
857               break;
858             }
859         }
860       if (old == '\0')
861         break;
862       pos = end + 1;
863     }
864   GNUNET_free (list);
865   return ret;
866 }
867
868
869 /**
870  * FIXME.
871  *
872  * @param value FIXME
873  * @return FIXME
874  */
875 static char *
876 escape_name (const char *value)
877 {
878   char *escaped;
879   const char *rpos;
880   char *wpos;
881
882   escaped = GNUNET_malloc (strlen (value) * 2 + 1);
883   memset (escaped, 0, strlen (value) * 2 + 1);
884   rpos = value;
885   wpos = escaped;
886   while (rpos[0] != '\0')
887     {
888       switch (rpos[0])
889         {
890         case '\\':
891         case ' ':
892           wpos[0] = '\\';
893           wpos[1] = rpos[0];
894           wpos += 2;
895           break;
896         default:
897           wpos[0] = rpos[0];
898           wpos++;
899         }
900       rpos++;
901     }
902   return escaped;
903 }
904
905
906 /**
907  * FIXME.
908  *
909  * @param cls string we compare with (const char*)
910  * @param fn filename we are currently looking at
911  * @return GNUNET_OK if the names do not match, GNUNET_SYSERR if they do
912  */
913 static int
914 test_match (void *cls, const char *fn)
915 {
916   const char *of = cls;
917   return (0 == strcmp (of, fn)) ? GNUNET_SYSERR : GNUNET_OK;
918 }
919
920
921 /**
922  * Append a filename to a configuration value that
923  * represents a list of filenames
924  *
925  * @param cfg configuration to update
926  * @param section section of interest
927  * @param option option of interest
928  * @param value filename to append
929  * @return GNUNET_OK on success,
930  *         GNUNET_NO if the filename already in the list
931  *         GNUNET_SYSERR on error
932  */
933 int
934 GNUNET_CONFIGURATION_append_value_filename (struct GNUNET_CONFIGURATION_Handle
935                                             *cfg,
936                                             const char *section,
937                                             const char *option,
938                                             const char *value)
939 {
940   char *escaped;
941   char *old;
942   char *nw;
943
944   if (GNUNET_SYSERR
945       == GNUNET_CONFIGURATION_iterate_value_filenames (cfg,
946                                                        section,
947                                                        option,
948                                                        &test_match,
949                                                        (void *) value))
950     return GNUNET_NO;           /* already exists */
951   if (GNUNET_OK !=
952       GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &old))
953     old = GNUNET_strdup ("");
954   escaped = escape_name (value);
955   nw = GNUNET_malloc (strlen (old) + strlen (escaped) + 2);
956   strcpy (nw, old);
957   strcat (nw, " ");
958   strcat (nw, escaped);
959   GNUNET_CONFIGURATION_set_value_string (cfg, section, option, nw);
960   GNUNET_free (old);
961   GNUNET_free (nw);
962   GNUNET_free (escaped);
963   return GNUNET_OK;
964 }
965
966
967 /**
968  * Remove a filename from a configuration value that
969  * represents a list of filenames
970  *
971  * @param cfg configuration to update
972  * @param section section of interest
973  * @param option option of interest
974  * @param value filename to remove
975  * @return GNUNET_OK on success,
976  *         GNUNET_NO if the filename is not in the list,
977  *         GNUNET_SYSERR on error
978  */
979 int
980 GNUNET_CONFIGURATION_remove_value_filename (struct GNUNET_CONFIGURATION_Handle
981                                             *cfg,
982                                             const char *section,
983                                             const char *option,
984                                             const char *value)
985 {
986   char *list;
987   char *pos;
988   char *end;
989   char *match;
990   char old;
991
992   if (GNUNET_OK !=
993       GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &list))
994     return GNUNET_NO;
995   match = escape_name (value);
996   pos = list;
997   while (1)
998     {
999       while (pos[0] == ' ')
1000         pos++;
1001       if (strlen (pos) == 0)
1002         break;
1003       end = pos + 1;
1004       while ((end[0] != ' ') && (end[0] != '\0'))
1005         {
1006           if (end[0] == '\\')
1007             {
1008               switch (end[1])
1009                 {
1010                 case '\\':
1011                 case ' ':
1012                   end++;
1013                   break;
1014                 case '\0':
1015                   /* illegal, but just keep it */
1016                   break;
1017                 default:
1018                   /* illegal, but just ignore that there was a '/' */
1019                   break;
1020                 }
1021             }
1022           end++;
1023         }
1024       old = end[0];
1025       end[0] = '\0';
1026       if (strlen (pos) > 0)
1027         {
1028           if (0 == strcmp (pos, match))
1029             {
1030               memmove (pos, &end[1], strlen (&end[1]) + 1);
1031
1032               if (pos != list)
1033                 pos[-1] = ' ';  /* previously changed to "\0" */
1034               GNUNET_CONFIGURATION_set_value_string (cfg,
1035                                                      section, option, list);
1036               GNUNET_free (list);
1037               GNUNET_free (match);
1038               return GNUNET_OK;
1039             }
1040         }
1041       if (old == '\0')
1042         break;
1043       pos = end + 1;
1044     }
1045   GNUNET_free (list);
1046   GNUNET_free (match);
1047   return GNUNET_NO;
1048 }
1049
1050
1051 /**
1052  * Load configuration (starts with defaults, then loads
1053  * system-specific configuration).
1054  *
1055  * @param cfg configuration to update
1056  * @param filename name of the configuration file
1057  * @return GNUNET_OK on success, GNUNET_SYSERR on error
1058  */
1059 int
1060 GNUNET_CONFIGURATION_load (struct GNUNET_CONFIGURATION_Handle *cfg,
1061                            const char *filename)
1062 {
1063   char *baseconfig;
1064   char *ipath;
1065
1066   ipath = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_DATADIR);
1067   if (ipath == NULL)
1068     return GNUNET_SYSERR;
1069   baseconfig = NULL;
1070   GNUNET_asprintf (&baseconfig,
1071                    "%s%s%s", ipath, DIR_SEPARATOR_STR, "defaults.conf");
1072   GNUNET_free (ipath);
1073   if ((GNUNET_OK !=
1074        GNUNET_CONFIGURATION_parse (cfg, baseconfig)) ||
1075       (!((filename == NULL) ||
1076          (GNUNET_OK == GNUNET_CONFIGURATION_parse (cfg, filename)))))
1077     {
1078       GNUNET_free (baseconfig);
1079       return GNUNET_SYSERR;
1080     }
1081   GNUNET_free (baseconfig);
1082   if ((GNUNET_YES == GNUNET_CONFIGURATION_have_value (cfg,
1083                                                       "TESTING",
1084                                                       "WEAKRANDOM")) &&
1085       (GNUNET_YES == GNUNET_CONFIGURATION_get_value_yesno (cfg,
1086                                                            "TESTING",
1087                                                            "WEAKRANDOM")))
1088     GNUNET_CRYPTO_random_disable_entropy_gathering ();
1089   return GNUNET_OK;
1090 }
1091
1092
1093
1094 /* end of configuration.c */