2541a79f455486d408f8324d003c550e99a64e7e
[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 GNUNET_CONFIGURATION_iterate (const struct GNUNET_CONFIGURATION_Handle *cfg,
363                                    GNUNET_CONFIGURATION_Iterator iter,
364                                    void *iter_cls)
365 {
366   struct ConfigSection *spos;
367   struct ConfigEntry *epos;
368
369   spos = cfg->sections;
370   while (spos != NULL)
371     {
372       epos = spos->entries;
373       while (epos != NULL)
374         {
375           iter (iter_cls, spos->name, epos->key, epos->val);
376           epos = epos->next;
377         }
378       spos = spos->next;
379     }
380 }
381
382
383 /**
384  * Copy a configuration value to the given target configuration.
385  * Overwrites existing entries.
386  *
387  * @param cls the destination configuration (struct GNUNET_CONFIGURATION_Handle*)
388  * @param section section for the value
389  * @param option option name of the value
390  * @param value value to copy 
391  */
392 static void
393 copy_entry (void *cls,
394             const char *section,
395             const char *option,
396             const char *value)
397 {
398   struct GNUNET_CONFIGURATION_Handle *dst = cls;
399   GNUNET_CONFIGURATION_set_value_string (dst, section, option, value);
400 }
401
402
403 /**
404  * Duplicate an existing configuration object.
405  *
406  * @param cfg configuration to duplicate
407  * @return duplicate configuration
408  */
409 struct GNUNET_CONFIGURATION_Handle *
410 GNUNET_CONFIGURATION_dup (const struct GNUNET_CONFIGURATION_Handle *cfg)
411 {
412   struct GNUNET_CONFIGURATION_Handle *ret;
413
414   ret = GNUNET_CONFIGURATION_create ();
415   GNUNET_CONFIGURATION_iterate (cfg, &copy_entry, ret);
416   return ret;
417 }
418
419
420 /**
421  * FIXME.
422  *
423  * @param cfg FIXME
424  * @param section FIXME
425  * @return matching entry, NULL if not found
426  */
427 static struct ConfigSection *
428 findSection (const struct GNUNET_CONFIGURATION_Handle *cfg, 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 GNUNET_CONFIGURATION_Handle
535                                        *cfg, const char *section,
536                                        const char *option,
537                                        unsigned long long *number)
538 {
539   struct ConfigEntry *e;
540
541   e = findEntry (cfg, section, option);
542   if (e == NULL)
543     return GNUNET_SYSERR;
544   if (1 != SSCANF (e->val, "%llu", number))
545     return GNUNET_SYSERR;
546   return GNUNET_OK;
547 }
548
549
550 /**
551  * Get a configuration value that should be a relative time.
552  *
553  * @param cfg configuration to inspect
554  * @param section section of interest
555  * @param option option of interest
556  * @param time set to the time value stored in the configuration
557  * @return GNUNET_OK on success, GNUNET_SYSERR on error
558  */
559 int
560 GNUNET_CONFIGURATION_get_value_time (const struct GNUNET_CONFIGURATION_Handle
561                                      *cfg, const char *section,
562                                      const char *option,
563                                      struct GNUNET_TIME_Relative *time)
564 {
565   unsigned long long num;
566   int ret;
567
568   ret = GNUNET_CONFIGURATION_get_value_number (cfg,
569                                                section,
570                                                option,
571                                                &num);
572   if (ret == GNUNET_OK)
573     time->value = (uint64_t) num;
574   return ret;
575 }
576
577
578 /**
579  * Get a configuration value that should be a string.
580  *
581  * @param cfg configuration to inspect
582  * @param section section of interest
583  * @param option option of interest
584  * @param value will be set to a freshly allocated configuration
585  *        value, or NULL if option is not specified
586  * @return GNUNET_OK on success, GNUNET_SYSERR on error
587  */
588 int
589 GNUNET_CONFIGURATION_get_value_string (const struct GNUNET_CONFIGURATION_Handle
590                                        *cfg, const char *section,
591                                        const char *option, char **value)
592 {
593   struct ConfigEntry *e;
594
595   e = findEntry (cfg, section, option);
596   if ((e == NULL) || (e->val == NULL))
597     {
598       *value = NULL;
599       return GNUNET_SYSERR;
600     }
601   *value = GNUNET_strdup (e->val);
602   return GNUNET_OK;
603 }
604
605
606 /**
607  * Get a configuration value that should be in a set of
608  * predefined strings
609  *
610  * @param cfg configuration to inspect
611  * @param section section of interest
612  * @param option option of interest
613  * @param choices NULL-terminated list of legal values
614  * @param value will be set to an entry in the legal list,
615  *        or NULL if option is not specified and no default given
616  * @return GNUNET_OK on success, GNUNET_SYSERR on error
617  */
618 int
619 GNUNET_CONFIGURATION_get_value_choice (const struct GNUNET_CONFIGURATION_Handle
620                                        *cfg, 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 *cfg,
660                                  const char *section, const char *option)
661 {
662   struct ConfigEntry *e;
663   if ((NULL == (e = findEntry (cfg, section, option))) || (e->val == NULL))
664     return GNUNET_NO;
665   return GNUNET_YES;
666 }
667
668
669 /**
670  * Expand an expression of the form "$FOO/BAR" to "DIRECTORY/BAR"
671  * where either in the "PATHS" section or the environtment
672  * "FOO" is set to "DIRECTORY".
673  *
674  * @param cfg configuration to use for path expansion
675  * @param orig string to $-expand (will be freed!)
676  * @return $-expanded string
677  */
678 char *
679 GNUNET_CONFIGURATION_expand_dollar (const struct GNUNET_CONFIGURATION_Handle *cfg,
680                                     char *orig)
681 {
682   int i;
683   char *prefix;
684   char *result;
685   const char *post;
686   const char *env;
687
688   if (orig[0] != '$')
689     return orig;
690   i = 0;
691   while ((orig[i] != '/') && (orig[i] != '\\') && (orig[i] != '\0'))
692     i++;
693   if (orig[i] == '\0')
694     {
695       post = "";
696     }
697   else
698     {
699       orig[i] = '\0';
700       post = &orig[i + 1];
701     }
702   if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (cfg,
703                                                           "PATHS",
704                                                           &orig[1], &prefix))
705     {
706       if (NULL == (env = getenv (&orig[1])))
707         {
708           orig[i] = DIR_SEPARATOR;
709           return orig;
710         }
711       prefix = GNUNET_strdup (env);
712     }
713   result = GNUNET_malloc (strlen (prefix) + strlen (post) + 2);
714   strcpy (result, prefix);
715   if ((strlen (prefix) == 0) ||
716       ((prefix[strlen (prefix) - 1] != DIR_SEPARATOR) && (strlen (post) > 0)))
717     strcat (result, DIR_SEPARATOR_STR);
718   strcat (result, post);
719   GNUNET_free (prefix);
720   GNUNET_free (orig);
721   return result;
722 }
723
724
725 /**
726  * Get a configuration value that should be a string.
727  *
728  * @param cfg configuration to inspect
729  * @param section section of interest
730  * @param option option of interest
731  * @param value will be set to a freshly allocated configuration
732  *        value, or NULL if option is not specified
733  * @return GNUNET_OK on success, GNUNET_SYSERR on error
734  */
735 int
736 GNUNET_CONFIGURATION_get_value_filename (const struct GNUNET_CONFIGURATION_Handle
737                                          *cfg, const char *section,
738                                          const char *option, char **value)
739 {
740   int ret;
741   char *tmp;
742
743   tmp = NULL;
744   ret = GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &tmp);
745   if (ret == GNUNET_SYSERR)
746     return ret;
747   if (tmp != NULL)
748     {
749       tmp = GNUNET_CONFIGURATION_expand_dollar (cfg, tmp);
750       *value = GNUNET_STRINGS_filename_expand (tmp);
751       GNUNET_free (tmp);
752       if (*value == NULL)
753         ret = GNUNET_SYSERR;
754     }
755   else
756     {
757       *value = NULL;
758     }
759   return ret;
760 }
761
762
763 /**
764  * Get a configuration value that should be in a set of
765  * "GNUNET_YES" or "GNUNET_NO".
766  *
767  * @param cfg configuration to inspect
768  * @param section section of interest
769  * @param option option of interest
770  * @return GNUNET_YES, GNUNET_NO or GNUNET_SYSERR
771  */
772 int
773 GNUNET_CONFIGURATION_get_value_yesno (const struct GNUNET_CONFIGURATION_Handle *cfg,
774                                       const char *section, const char *option)
775 {
776   static const char *yesno[] = { "YES", "NO", NULL };
777   const char *val;
778   int ret;
779
780   ret = GNUNET_CONFIGURATION_get_value_choice (cfg,
781                                                section, option, yesno, &val);
782   if (ret == GNUNET_SYSERR)
783     return ret;
784   if (val == yesno[0])
785     return GNUNET_YES;
786   return GNUNET_NO;
787 }
788
789
790 /**
791  * Iterate over the set of filenames stored in a configuration value.
792  *
793  * @param cfg configuration to inspect
794  * @param section section of interest
795  * @param option option of interest
796  * @param cb function to call on each filename
797  * @param cb_cls closure for cb
798  * @return number of filenames iterated over, -1 on error
799  */
800 int
801 GNUNET_CONFIGURATION_iterate_value_filenames (const struct
802                                               GNUNET_CONFIGURATION_Handle
803                                               *cfg, const char *section,
804                                               const char *option,
805                                               GNUNET_FileNameCallback cb,
806                                               void *cb_cls)
807 {
808   char *list;
809   char *pos;
810   char *end;
811   char old;
812   int ret;
813
814   if (GNUNET_OK !=
815       GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &list))
816     return 0;
817   GNUNET_assert (list != NULL);
818   ret = 0;
819   pos = list;
820   while (1)
821     {
822       while (pos[0] == ' ')
823         pos++;
824       if (strlen (pos) == 0)
825         break;
826       end = pos + 1;
827       while ((end[0] != ' ') && (end[0] != '\0'))
828         {
829           if (end[0] == '\\')
830             {
831               switch (end[1])
832                 {
833                 case '\\':
834                 case ' ':
835                   memmove (end, &end[1], strlen (&end[1]) + 1);
836                 case '\0':
837                   /* illegal, but just keep it */
838                   break;
839                 default:
840                   /* illegal, but just ignore that there was a '/' */
841                   break;
842                 }
843             }
844           end++;
845         }
846       old = end[0];
847       end[0] = '\0';
848       if (strlen (pos) > 0)
849         {
850           ret++;
851           if ((cb != NULL) && (GNUNET_OK != cb (cb_cls, pos)))
852             {
853               ret = GNUNET_SYSERR;
854               break;
855             }
856         }
857       if (old == '\0')
858         break;
859       pos = end + 1;
860     }
861   GNUNET_free (list);
862   return ret;
863 }
864
865
866 /**
867  * FIXME.
868  *
869  * @param value FIXME
870  * @return FIXME
871  */
872 static char *
873 escape_name (const char *value)
874 {
875   char *escaped;
876   const char *rpos;
877   char *wpos;
878
879   escaped = GNUNET_malloc (strlen (value) * 2 + 1);
880   memset (escaped, 0, strlen (value) * 2 + 1);
881   rpos = value;
882   wpos = escaped;
883   while (rpos[0] != '\0')
884     {
885       switch (rpos[0])
886         {
887         case '\\':
888         case ' ':
889           wpos[0] = '\\';
890           wpos[1] = rpos[0];
891           wpos += 2;
892           break;
893         default:
894           wpos[0] = rpos[0];
895           wpos++;
896         }
897       rpos++;
898     }
899   return escaped;
900 }
901
902
903 /**
904  * FIXME.
905  *
906  * @param cls string we compare with (const char*)
907  * @param fn filename we are currently looking at
908  * @return GNUNET_OK if the names do not match, GNUNET_SYSERR if they do
909  */
910 static int
911 test_match (void *cls, const char *fn)
912 {
913   const char *of = cls;
914   return (0 == strcmp (of, fn)) ? GNUNET_SYSERR : GNUNET_OK;
915 }
916
917
918 /**
919  * Append a filename to a configuration value that
920  * represents a list of filenames
921  *
922  * @param cfg configuration to update
923  * @param section section of interest
924  * @param option option of interest
925  * @param value filename to append
926  * @return GNUNET_OK on success,
927  *         GNUNET_NO if the filename already in the list
928  *         GNUNET_SYSERR on error
929  */
930 int
931 GNUNET_CONFIGURATION_append_value_filename (struct GNUNET_CONFIGURATION_Handle
932                                             *cfg,
933                                             const char *section,
934                                             const char *option,
935                                             const char *value)
936 {
937   char *escaped;
938   char *old;
939   char *nw;
940
941   if (GNUNET_SYSERR
942       == GNUNET_CONFIGURATION_iterate_value_filenames (cfg,
943                                                        section,
944                                                        option,
945                                                        &test_match,
946                                                        (void *) value))
947     return GNUNET_NO;           /* already exists */
948   if (GNUNET_OK !=
949       GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &old))
950     old = GNUNET_strdup ("");
951   escaped = escape_name (value);
952   nw = GNUNET_malloc (strlen (old) + strlen (escaped) + 2);
953   strcpy (nw, old);
954   strcat (nw, " ");
955   strcat (nw, escaped);
956   GNUNET_CONFIGURATION_set_value_string (cfg, section, option, nw);
957   GNUNET_free (old);
958   GNUNET_free (nw);
959   GNUNET_free (escaped);
960   return GNUNET_OK;
961 }
962
963
964 /**
965  * Remove a filename from a configuration value that
966  * represents a list of filenames
967  *
968  * @param cfg configuration to update
969  * @param section section of interest
970  * @param option option of interest
971  * @param value filename to remove
972  * @return GNUNET_OK on success,
973  *         GNUNET_NO if the filename is not in the list,
974  *         GNUNET_SYSERR on error
975  */
976 int
977 GNUNET_CONFIGURATION_remove_value_filename (struct GNUNET_CONFIGURATION_Handle
978                                             *cfg,
979                                             const char *section,
980                                             const char *option,
981                                             const char *value)
982 {
983   char *list;
984   char *pos;
985   char *end;
986   char *match;
987   char old;
988
989   if (GNUNET_OK !=
990       GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &list))
991     return GNUNET_NO;
992   match = escape_name (value);
993   pos = list;
994   while (1)
995     {
996       while (pos[0] == ' ')
997         pos++;
998       if (strlen (pos) == 0)
999         break;
1000       end = pos + 1;
1001       while ((end[0] != ' ') && (end[0] != '\0'))
1002         {
1003           if (end[0] == '\\')
1004             {
1005               switch (end[1])
1006                 {
1007                 case '\\':
1008                 case ' ':
1009                   end++;
1010                   break;
1011                 case '\0':
1012                   /* illegal, but just keep it */
1013                   break;
1014                 default:
1015                   /* illegal, but just ignore that there was a '/' */
1016                   break;
1017                 }
1018             }
1019           end++;
1020         }
1021       old = end[0];
1022       end[0] = '\0';
1023       if (strlen (pos) > 0)
1024         {
1025           if (0 == strcmp (pos, match))
1026             {
1027               memmove (pos, &end[1], strlen (&end[1]) + 1);
1028
1029               if (pos != list)
1030                 pos[-1] = ' ';  /* previously changed to "\0" */
1031               GNUNET_CONFIGURATION_set_value_string (cfg,
1032                                                      section, option, list);
1033               GNUNET_free (list);
1034               GNUNET_free (match);
1035               return GNUNET_OK;
1036             }
1037         }
1038       if (old == '\0')
1039         break;
1040       pos = end + 1;
1041     }
1042   GNUNET_free (list);
1043   GNUNET_free (match);
1044   return GNUNET_NO;
1045 }
1046
1047
1048 /**
1049  * Load configuration (starts with defaults, then loads
1050  * system-specific configuration).
1051  *
1052  * @param cfg configuration to update
1053  * @param filename name of the configuration file
1054  * @return GNUNET_OK on success, GNUNET_SYSERR on error
1055  */
1056 int
1057 GNUNET_CONFIGURATION_load (struct GNUNET_CONFIGURATION_Handle *cfg,
1058                            const char *filename)
1059 {
1060   char *baseconfig;
1061   char *ipath;
1062
1063   ipath = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_DATADIR);
1064   if (ipath == NULL)
1065     return GNUNET_SYSERR;
1066   baseconfig = NULL;
1067   GNUNET_asprintf (&baseconfig,
1068                    "%s%s%s", ipath, DIR_SEPARATOR_STR, "defaults.conf");
1069   GNUNET_free (ipath);
1070   if ((GNUNET_OK !=
1071        GNUNET_CONFIGURATION_parse (cfg, baseconfig)) ||
1072       (!((filename == NULL) ||
1073          (GNUNET_OK == GNUNET_CONFIGURATION_parse (cfg, filename)))))
1074     {
1075       GNUNET_free (baseconfig);
1076       return GNUNET_SYSERR;
1077     }
1078   GNUNET_free (baseconfig);
1079   if ((GNUNET_YES == GNUNET_CONFIGURATION_have_value (cfg,
1080                                                       "TESTING",
1081                                                       "WEAKRANDOM")) &&
1082       (GNUNET_YES == GNUNET_CONFIGURATION_get_value_yesno (cfg,
1083                                                            "TESTING",
1084                                                            "WEAKRANDOM")))
1085     GNUNET_CRYPTO_random_disable_entropy_gathering ();
1086   return GNUNET_OK;
1087 }
1088
1089
1090
1091 /* end of configuration.c */