-consistently use struct GNUNET_HashCode
[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  * @author Christian Grothoff
25  */
26
27 #include "platform.h"
28 #include "gnunet_common.h"
29 #include "gnunet_util_lib.h"
30 #include "gnunet_crypto_lib.h"
31 #include "gnunet_strings_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 *cfgDefault;
109   struct GNUNET_CONFIGURATION_Handle *cfgDiff;
110 };
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_malloc (sizeof (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  * 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   if (fn == NULL)
168     return GNUNET_SYSERR;
169   dirty = cfg->dirty;           /* back up value! */
170   if (NULL == (fp = FOPEN (fn, "r")))
171   {
172     LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "fopen", fn);
173     GNUNET_free (fn);
174     return GNUNET_SYSERR;
175   }
176   GNUNET_free (fn);
177   ret = GNUNET_OK;
178   section = GNUNET_strdup ("");
179   memset (line, 0, 256);
180   nr = 0;
181   while (NULL != fgets (line, 255, fp))
182   {
183     nr++;
184     for (i = 0; i < 255; i++)
185       if (line[i] == '\t')
186         line[i] = ' ';
187     if (line[0] == '\n' || line[0] == '#' || line[0] == '%' || line[0] == '\r')
188       continue;
189     emptyline = 1;
190     for (i = 0; (i < 255 && line[i] != 0); i++)
191       if (line[i] != ' ' && line[i] != '\n' && line[i] != '\r')
192         emptyline = 0;
193     if (emptyline == 1)
194       continue;
195     /* remove tailing whitespace */
196     for (i = strlen (line) - 1; (i >= 0) && (isspace ((unsigned char) line[i]));
197          i--)
198       line[i] = '\0';
199     if (1 == SSCANF (line, "@INLINE@ %191[^\n]", value))
200     {
201       /* @INLINE@ value */
202       if (GNUNET_OK != GNUNET_CONFIGURATION_parse (cfg, value))
203         ret = GNUNET_SYSERR;    /* failed to parse included config */
204     }
205     else if (1 == SSCANF (line, "[%99[^]]]", value))
206     {
207       /* [value] */
208       GNUNET_free (section);
209       section = GNUNET_strdup (value);
210     }
211     else if (2 == SSCANF (line, " %63[^= ] = %191[^\n]", tag, value))
212     {
213       /* tag = value */
214       /* Strip LF */
215       i = strlen (value) - 1;
216       while ((i >= 0) && (isspace ((unsigned char) value[i])))
217         value[i--] = '\0';
218       /* remove quotes */
219       i = 0;
220       if (value[0] == '"')
221       {
222         i = 1;
223         while ((value[i] != '\0') && (value[i] != '"'))
224           i++;
225         if (value[i] == '"')
226         {
227           value[i] = '\0';
228           i = 1;
229         }
230         else
231           i = 0;
232       }
233       GNUNET_CONFIGURATION_set_value_string (cfg, section, tag, &value[i]);
234     }
235     else if (1 == SSCANF (line, " %63[^= ] =[^\n]", tag))
236     {
237       /* tag = */
238       GNUNET_CONFIGURATION_set_value_string (cfg, section, tag, "");
239     }
240     else
241     {
242       /* parse error */
243       LOG (GNUNET_ERROR_TYPE_WARNING,
244            _("Syntax error in configuration file `%s' at line %u.\n"), filename,
245            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   if (fn == NULL)
294     return GNUNET_SYSERR;
295   if (GNUNET_OK != GNUNET_DISK_directory_create_for_file (fn))
296   {
297     GNUNET_free (fn);
298     return GNUNET_SYSERR;
299   }
300   if (NULL == (fp = FOPEN (fn, "w")))
301   {
302     LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "fopen", fn);
303     GNUNET_free (fn);
304     return GNUNET_SYSERR;
305   }
306   GNUNET_free (fn);
307   error = 0;
308   sec = cfg->sections;
309   while (sec != NULL)
310   {
311     if (0 > FPRINTF (fp, "[%s]\n", sec->name))
312     {
313       error = 1;
314       break;
315     }
316     ent = sec->entries;
317     while (ent != NULL)
318     {
319       if (ent->val != NULL)
320       {
321         val = GNUNET_malloc (strlen (ent->val) * 2 + 1);
322         strcpy (val, ent->val);
323         while (NULL != (pos = strstr (val, "\n")))
324         {
325           memmove (&pos[2], &pos[1], strlen (&pos[1]));
326           pos[0] = '\\';
327           pos[1] = 'n';
328         }
329         if (0 > FPRINTF (fp, "%s = %s\n", ent->key, val))
330         {
331           error = 1;
332           GNUNET_free (val);
333           break;
334         }
335         GNUNET_free (val);
336       }
337       ent = ent->next;
338     }
339     if (error != 0)
340       break;
341     if (0 > FPRINTF (fp, "%s\n", ""))
342     {
343       error = 1;
344       break;
345     }
346     sec = sec->next;
347   }
348   if (error != 0)
349     LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "fprintf", filename);
350   GNUNET_assert (0 == FCLOSE (fp));
351   if (error != 0)
352   {
353     cfg->dirty = GNUNET_SYSERR; /* last write failed */
354     return GNUNET_SYSERR;
355   }
356   cfg->dirty = GNUNET_NO;       /* last write succeeded */
357   return GNUNET_OK;
358 }
359
360
361 /**
362  * Iterate over all options in the configuration.
363  *
364  * @param cfg configuration to inspect
365  * @param iter function to call on each option
366  * @param iter_cls closure for iter
367  */
368 void
369 GNUNET_CONFIGURATION_iterate (const struct GNUNET_CONFIGURATION_Handle *cfg,
370                               GNUNET_CONFIGURATION_Iterator iter,
371                               void *iter_cls)
372 {
373   struct ConfigSection *spos;
374   struct ConfigEntry *epos;
375
376   spos = cfg->sections;
377   while (spos != NULL)
378   {
379     epos = spos->entries;
380     while (epos != NULL)
381     {
382       iter (iter_cls, spos->name, epos->key, epos->val);
383       epos = epos->next;
384     }
385     spos = spos->next;
386   }
387 }
388
389
390 /**
391  * Iterate over values of a section in the configuration.
392  *
393  * @param cfg configuration to inspect
394  * @param section the section
395  * @param iter function to call on each option
396  * @param iter_cls closure for iter
397  */
398 void
399 GNUNET_CONFIGURATION_iterate_section_values (const struct
400                                              GNUNET_CONFIGURATION_Handle *cfg,
401                                              const char *section,
402                                              GNUNET_CONFIGURATION_Iterator iter,
403                                              void *iter_cls)
404 {
405   struct ConfigSection *spos;
406   struct ConfigEntry *epos;
407
408   spos = cfg->sections;
409   while ((spos != NULL) && (0 != strcasecmp (spos->name, section)))
410     spos = spos->next;
411
412   if (spos == NULL)
413     return;
414
415   epos = spos->entries;
416   while (epos != NULL)
417   {
418     iter (iter_cls, spos->name, epos->key, epos->val);
419     epos = epos->next;
420   }
421 }
422
423
424 /**
425  * Iterate over all sections in the configuration.
426  *
427  * @param cfg configuration to inspect
428  * @param iter function to call on each section
429  * @param iter_cls closure for iter
430  */
431 void
432 GNUNET_CONFIGURATION_iterate_sections (const struct GNUNET_CONFIGURATION_Handle
433                                        *cfg,
434                                        GNUNET_CONFIGURATION_Section_Iterator
435                                        iter, void *iter_cls)
436 {
437   struct ConfigSection *spos;
438   struct ConfigSection *next;
439
440   next = cfg->sections;
441   while (next != NULL)
442   {
443     spos = next;
444     next = spos->next;
445     iter (iter_cls, spos->name);
446   }
447 }
448
449 /**
450  * Remove the given section and all options in it.
451  *
452  * @param cfg configuration to inspect
453  * @param section name of the section to remove
454  */
455 void
456 GNUNET_CONFIGURATION_remove_section (struct GNUNET_CONFIGURATION_Handle *cfg,
457                                      const char *section)
458 {
459   struct ConfigSection *spos;
460   struct ConfigSection *prev;
461   struct ConfigEntry *ent;
462
463   prev = NULL;
464   spos = cfg->sections;
465   while (spos != NULL)
466   {
467     if (0 == strcasecmp (section, spos->name))
468     {
469       if (prev == NULL)
470         cfg->sections = spos->next;
471       else
472         prev->next = spos->next;
473       while (NULL != (ent = spos->entries))
474       {
475         spos->entries = ent->next;
476         GNUNET_free (ent->key);
477         GNUNET_free_non_null (ent->val);
478         GNUNET_free (ent);
479         cfg->dirty = GNUNET_YES;
480       }
481       GNUNET_free (spos->name);
482       GNUNET_free (spos);
483       return;
484     }
485     prev = spos;
486     spos = spos->next;
487   }
488 }
489
490
491 /**
492  * Copy a configuration value to the given target configuration.
493  * Overwrites existing entries.
494  *
495  * @param cls the destination configuration (struct GNUNET_CONFIGURATION_Handle*)
496  * @param section section for the value
497  * @param option option name of the value
498  * @param value value to copy
499  */
500 static void
501 copy_entry (void *cls, const char *section, const char *option,
502             const char *value)
503 {
504   struct GNUNET_CONFIGURATION_Handle *dst = cls;
505
506   GNUNET_CONFIGURATION_set_value_string (dst, section, option, value);
507 }
508
509
510 /**
511  * Duplicate an existing configuration object.
512  *
513  * @param cfg configuration to duplicate
514  * @return duplicate configuration
515  */
516 struct GNUNET_CONFIGURATION_Handle *
517 GNUNET_CONFIGURATION_dup (const struct GNUNET_CONFIGURATION_Handle *cfg)
518 {
519   struct GNUNET_CONFIGURATION_Handle *ret;
520
521   ret = GNUNET_CONFIGURATION_create ();
522   GNUNET_CONFIGURATION_iterate (cfg, &copy_entry, ret);
523   return ret;
524 }
525
526
527 /**
528  * FIXME.
529  *
530  * @param cfg FIXME
531  * @param section FIXME
532  * @return matching entry, NULL if not found
533  */
534 static struct ConfigSection *
535 findSection (const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section)
536 {
537   struct ConfigSection *pos;
538
539   pos = cfg->sections;
540   while ((pos != NULL) && (0 != strcasecmp (section, pos->name)))
541     pos = pos->next;
542   return pos;
543 }
544
545
546 /**
547  * Find an entry from a configuration.
548  *
549  * @param cfg handle to the configuration
550  * @param section section the option is in
551  * @param key the option
552  * @return matching entry, NULL if not found
553  */
554 static struct ConfigEntry *
555 findEntry (const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section,
556            const char *key)
557 {
558   struct ConfigSection *sec;
559   struct ConfigEntry *pos;
560
561   sec = findSection (cfg, section);
562   if (sec == NULL)
563     return NULL;
564   pos = sec->entries;
565   while ((pos != NULL) && (0 != strcasecmp (key, pos->key)))
566     pos = pos->next;
567   return pos;
568 }
569
570
571 /**
572  * A callback function, compares entries from two configurations
573  * (default against a new configuration) and write the diffs in a
574  * diff-configuration object (the callback object).
575  *
576  * @param cls the diff configuration (struct DiffHandle*)
577  * @param section section for the value (of the default conf.)
578  * @param option option name of the value (of the default conf.)
579  * @param value value to copy (of the default conf.)
580  */
581 static void
582 compareEntries (void *cls, const char *section, const char *option,
583                 const char *value)
584 {
585   struct DiffHandle *dh = cls;
586   struct ConfigEntry *entNew;
587
588   entNew = findEntry (dh->cfgDefault, section, option);
589   if ((entNew != NULL) && (strcmp (entNew->val, value) == 0))
590     return;
591   GNUNET_CONFIGURATION_set_value_string (dh->cfgDiff, section, option, value);
592 }
593
594
595 /**
596  * Write only configuration entries that have been changed to configuration file
597  * @param cfgDefault default configuration
598  * @param cfgNew new configuration
599  * @param filename where to write the configuration diff between default and new
600  * @return GNUNET_OK on success, GNUNET_SYSERR on error
601  */
602 int
603 GNUNET_CONFIGURATION_write_diffs (const struct GNUNET_CONFIGURATION_Handle
604                                   *cfgDefault,
605                                   const struct GNUNET_CONFIGURATION_Handle
606                                   *cfgNew, const char *filename)
607 {
608   int ret;
609   struct DiffHandle diffHandle;
610
611   diffHandle.cfgDiff = GNUNET_CONFIGURATION_create ();
612   diffHandle.cfgDefault = cfgDefault;
613   GNUNET_CONFIGURATION_iterate (cfgNew, compareEntries, &diffHandle);
614   ret = GNUNET_CONFIGURATION_write (diffHandle.cfgDiff, filename);
615   GNUNET_CONFIGURATION_destroy (diffHandle.cfgDiff);
616   return ret;
617 }
618
619
620 /**
621  * Set a configuration value that should be a string.
622  *
623  * @param cfg configuration to update
624  * @param section section of interest
625  * @param option option of interest
626  * @param value value to set
627  */
628 void
629 GNUNET_CONFIGURATION_set_value_string (struct GNUNET_CONFIGURATION_Handle *cfg,
630                                        const char *section, const char *option,
631                                        const char *value)
632 {
633   struct ConfigSection *sec;
634   struct ConfigEntry *e;
635   char *nv;
636
637   e = findEntry (cfg, section, option);
638   if (e != NULL)
639   {
640     nv = GNUNET_strdup (value);
641     GNUNET_free_non_null (e->val);
642     e->val = nv;
643     return;
644   }
645   sec = findSection (cfg, section);
646   if (sec == NULL)
647   {
648     sec = GNUNET_malloc (sizeof (struct ConfigSection));
649     sec->name = GNUNET_strdup (section);
650     sec->next = cfg->sections;
651     cfg->sections = sec;
652   }
653   e = GNUNET_malloc (sizeof (struct ConfigEntry));
654   e->key = GNUNET_strdup (option);
655   e->val = GNUNET_strdup (value);
656   e->next = sec->entries;
657   sec->entries = e;
658 }
659
660
661 /**
662  * Set a configuration value that should be a number.
663  *
664  * @param cfg configuration to update
665  * @param section section of interest
666  * @param option option of interest
667  * @param number value to set
668  */
669 void
670 GNUNET_CONFIGURATION_set_value_number (struct GNUNET_CONFIGURATION_Handle *cfg,
671                                        const char *section, const char *option,
672                                        unsigned long long number)
673 {
674   char s[64];
675
676   GNUNET_snprintf (s, 64, "%llu", number);
677   GNUNET_CONFIGURATION_set_value_string (cfg, section, option, s);
678 }
679
680
681 /**
682  * Get a configuration value that should be a number.
683  *
684  * @param cfg configuration to inspect
685  * @param section section of interest
686  * @param option option of interest
687  * @param number where to store the numeric value of the option
688  * @return GNUNET_OK on success, GNUNET_SYSERR on error
689  */
690 int
691 GNUNET_CONFIGURATION_get_value_number (const struct GNUNET_CONFIGURATION_Handle
692                                        *cfg, const char *section,
693                                        const char *option,
694                                        unsigned long long *number)
695 {
696   struct ConfigEntry *e;
697
698   e = findEntry (cfg, section, option);
699   if (e == NULL)
700     return GNUNET_SYSERR;
701   if (1 != SSCANF (e->val, "%llu", number))
702     return GNUNET_SYSERR;
703   return GNUNET_OK;
704 }
705
706
707 /**
708  * Get a configuration value that should be a relative time.
709  *
710  * @param cfg configuration to inspect
711  * @param section section of interest
712  * @param option option of interest
713  * @param time set to the time value stored in the configuration
714  * @return GNUNET_OK on success, GNUNET_SYSERR on error
715  */
716 int
717 GNUNET_CONFIGURATION_get_value_time (const struct GNUNET_CONFIGURATION_Handle
718                                      *cfg, const char *section,
719                                      const char *option,
720                                      struct GNUNET_TIME_Relative *time)
721 {
722   struct ConfigEntry *e;
723
724   e = findEntry (cfg, section, option);
725   if (e == NULL)
726     return GNUNET_SYSERR;
727
728   return GNUNET_STRINGS_fancy_time_to_relative (e->val, time);
729 }
730
731
732 /**
733  * Get a configuration value that should be a size in bytes.
734  *
735  * @param cfg configuration to inspect
736  * @param section section of interest
737  * @param option option of interest
738  * @param size set to the size in bytes as stored in the configuration
739  * @return GNUNET_OK on success, GNUNET_SYSERR on error
740  */
741 int
742 GNUNET_CONFIGURATION_get_value_size (const struct GNUNET_CONFIGURATION_Handle
743                                      *cfg, const char *section,
744                                      const char *option,
745                                      unsigned long long *size)
746 {
747   struct ConfigEntry *e;
748
749   e = findEntry (cfg, section, option);
750   if (e == NULL)
751     return GNUNET_SYSERR;
752   return GNUNET_STRINGS_fancy_size_to_bytes (e->val, size);
753 }
754
755
756 /**
757  * Get a configuration value that should be a string.
758  *
759  * @param cfg configuration to inspect
760  * @param section section of interest
761  * @param option option of interest
762  * @param value will be set to a freshly allocated configuration
763  *        value, or NULL if option is not specified
764  * @return GNUNET_OK on success, GNUNET_SYSERR on error
765  */
766 int
767 GNUNET_CONFIGURATION_get_value_string (const struct GNUNET_CONFIGURATION_Handle
768                                        *cfg, const char *section,
769                                        const char *option, char **value)
770 {
771   struct ConfigEntry *e;
772
773   e = findEntry (cfg, section, option);
774   if ((e == NULL) || (e->val == NULL))
775   {
776     *value = NULL;
777     return GNUNET_SYSERR;
778   }
779   *value = GNUNET_strdup (e->val);
780   return GNUNET_OK;
781 }
782
783
784 /**
785  * Get a configuration value that should be in a set of
786  * predefined strings
787  *
788  * @param cfg configuration to inspect
789  * @param section section of interest
790  * @param option option of interest
791  * @param choices NULL-terminated list of legal values
792  * @param value will be set to an entry in the legal list,
793  *        or NULL if option is not specified and no default given
794  * @return GNUNET_OK on success, GNUNET_SYSERR on error
795  */
796 int
797 GNUNET_CONFIGURATION_get_value_choice (const struct GNUNET_CONFIGURATION_Handle
798                                        *cfg, const char *section,
799                                        const char *option, const char **choices,
800                                        const char **value)
801 {
802   struct ConfigEntry *e;
803   int i;
804
805   e = findEntry (cfg, section, option);
806   if (e == NULL)
807     return GNUNET_SYSERR;
808   i = 0;
809   while (choices[i] != NULL)
810   {
811     if (0 == strcasecmp (choices[i], e->val))
812       break;
813     i++;
814   }
815   if (choices[i] == NULL)
816   {
817     LOG (GNUNET_ERROR_TYPE_ERROR,
818          _("Configuration value '%s' for '%s'"
819            " in section '%s' is not in set of legal choices\n"), e->val, option,
820          section);
821     return GNUNET_SYSERR;
822   }
823   *value = choices[i];
824   return GNUNET_OK;
825 }
826
827
828 /**
829  * Test if we have a value for a particular option
830  * @param cfg configuration to inspect
831  * @param section section of interest
832  * @param option option of interest
833  * @return GNUNET_YES if so, GNUNET_NO if not.
834  */
835 int
836 GNUNET_CONFIGURATION_have_value (const struct GNUNET_CONFIGURATION_Handle *cfg,
837                                  const char *section, const char *option)
838 {
839   struct ConfigEntry *e;
840
841   if ((NULL == (e = findEntry (cfg, section, option))) || (e->val == NULL))
842     return GNUNET_NO;
843   return GNUNET_YES;
844 }
845
846
847 /**
848  * Expand an expression of the form "$FOO/BAR" to "DIRECTORY/BAR"
849  * where either in the "PATHS" section or the environtment
850  * "FOO" is set to "DIRECTORY".
851  *
852  * @param cfg configuration to use for path expansion
853  * @param orig string to $-expand (will be freed!)
854  * @return $-expanded string
855  */
856 char *
857 GNUNET_CONFIGURATION_expand_dollar (const struct GNUNET_CONFIGURATION_Handle
858                                     *cfg, char *orig)
859 {
860   int i;
861   char *prefix;
862   char *result;
863   const char *post;
864   const char *env;
865
866   if (orig[0] != '$')
867     return orig;
868   i = 0;
869   while ((orig[i] != '/') && (orig[i] != '\\') && (orig[i] != '\0'))
870     i++;
871   if (orig[i] == '\0')
872   {
873     post = "";
874   }
875   else
876   {
877     orig[i] = '\0';
878     post = &orig[i + 1];
879   }
880   if (GNUNET_OK !=
881       GNUNET_CONFIGURATION_get_value_filename (cfg, "PATHS", &orig[1], &prefix))
882   {
883     if (NULL == (env = getenv (&orig[1])))
884     {
885       orig[i] = DIR_SEPARATOR;
886       return orig;
887     }
888     prefix = GNUNET_strdup (env);
889   }
890   result = GNUNET_malloc (strlen (prefix) + strlen (post) + 2);
891   strcpy (result, prefix);
892   if ((strlen (prefix) == 0) ||
893       ((prefix[strlen (prefix) - 1] != DIR_SEPARATOR) && (strlen (post) > 0)))
894     strcat (result, DIR_SEPARATOR_STR);
895   strcat (result, post);
896   GNUNET_free (prefix);
897   GNUNET_free (orig);
898   return result;
899 }
900
901
902 /**
903  * Get a configuration value that should be a string.
904  *
905  * @param cfg configuration to inspect
906  * @param section section of interest
907  * @param option option of interest
908  * @param value will be set to a freshly allocated configuration
909  *        value, or NULL if option is not specified
910  * @return GNUNET_OK on success, GNUNET_SYSERR on error
911  */
912 int
913 GNUNET_CONFIGURATION_get_value_filename (const struct
914                                          GNUNET_CONFIGURATION_Handle *cfg,
915                                          const char *section,
916                                          const char *option, char **value)
917 {
918   char *tmp;
919
920   if (GNUNET_OK !=
921       GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &tmp))
922   {
923     *value = NULL;
924     return GNUNET_SYSERR;
925   }
926   tmp = GNUNET_CONFIGURATION_expand_dollar (cfg, tmp);
927   *value = GNUNET_STRINGS_filename_expand (tmp);
928   GNUNET_free (tmp);
929   if (*value == NULL)
930     return GNUNET_SYSERR;
931   return GNUNET_OK;
932 }
933
934
935 /**
936  * Get a configuration value that should be in a set of
937  * "GNUNET_YES" or "GNUNET_NO".
938  *
939  * @param cfg configuration to inspect
940  * @param section section of interest
941  * @param option option of interest
942  * @return GNUNET_YES, GNUNET_NO or GNUNET_SYSERR
943  */
944 int
945 GNUNET_CONFIGURATION_get_value_yesno (const struct GNUNET_CONFIGURATION_Handle
946                                       *cfg, const char *section,
947                                       const char *option)
948 {
949   static const char *yesno[] = { "YES", "NO", NULL };
950   const char *val;
951   int ret;
952
953   ret =
954       GNUNET_CONFIGURATION_get_value_choice (cfg, section, option, yesno, &val);
955   if (ret == GNUNET_SYSERR)
956     return ret;
957   if (val == yesno[0])
958     return GNUNET_YES;
959   return GNUNET_NO;
960 }
961
962
963 /**
964  * Iterate over the set of filenames stored in a configuration value.
965  *
966  * @param cfg configuration to inspect
967  * @param section section of interest
968  * @param option option of interest
969  * @param cb function to call on each filename
970  * @param cb_cls closure for cb
971  * @return number of filenames iterated over, -1 on error
972  */
973 int
974 GNUNET_CONFIGURATION_iterate_value_filenames (const struct
975                                               GNUNET_CONFIGURATION_Handle *cfg,
976                                               const char *section,
977                                               const char *option,
978                                               GNUNET_FileNameCallback cb,
979                                               void *cb_cls)
980 {
981   char *list;
982   char *pos;
983   char *end;
984   char old;
985   int ret;
986
987   if (GNUNET_OK !=
988       GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &list))
989     return 0;
990   GNUNET_assert (list != NULL);
991   ret = 0;
992   pos = list;
993   while (1)
994   {
995     while (pos[0] == ' ')
996       pos++;
997     if (strlen (pos) == 0)
998       break;
999     end = pos + 1;
1000     while ((end[0] != ' ') && (end[0] != '\0'))
1001     {
1002       if (end[0] == '\\')
1003       {
1004         switch (end[1])
1005         {
1006         case '\\':
1007         case ' ':
1008           memmove (end, &end[1], strlen (&end[1]) + 1);
1009         case '\0':
1010           /* illegal, but just keep it */
1011           break;
1012         default:
1013           /* illegal, but just ignore that there was a '/' */
1014           break;
1015         }
1016       }
1017       end++;
1018     }
1019     old = end[0];
1020     end[0] = '\0';
1021     if (strlen (pos) > 0)
1022     {
1023       ret++;
1024       if ((cb != NULL) && (GNUNET_OK != cb (cb_cls, pos)))
1025       {
1026         ret = GNUNET_SYSERR;
1027         break;
1028       }
1029     }
1030     if (old == '\0')
1031       break;
1032     pos = end + 1;
1033   }
1034   GNUNET_free (list);
1035   return ret;
1036 }
1037
1038
1039 /**
1040  * FIXME.
1041  *
1042  * @param value FIXME
1043  * @return FIXME
1044  */
1045 static char *
1046 escape_name (const char *value)
1047 {
1048   char *escaped;
1049   const char *rpos;
1050   char *wpos;
1051
1052   escaped = GNUNET_malloc (strlen (value) * 2 + 1);
1053   memset (escaped, 0, strlen (value) * 2 + 1);
1054   rpos = value;
1055   wpos = escaped;
1056   while (rpos[0] != '\0')
1057   {
1058     switch (rpos[0])
1059     {
1060     case '\\':
1061     case ' ':
1062       wpos[0] = '\\';
1063       wpos[1] = rpos[0];
1064       wpos += 2;
1065       break;
1066     default:
1067       wpos[0] = rpos[0];
1068       wpos++;
1069     }
1070     rpos++;
1071   }
1072   return escaped;
1073 }
1074
1075
1076 /**
1077  * FIXME.
1078  *
1079  * @param cls string we compare with (const char*)
1080  * @param fn filename we are currently looking at
1081  * @return GNUNET_OK if the names do not match, GNUNET_SYSERR if they do
1082  */
1083 static int
1084 test_match (void *cls, const char *fn)
1085 {
1086   const char *of = cls;
1087
1088   return (0 == strcmp (of, fn)) ? GNUNET_SYSERR : GNUNET_OK;
1089 }
1090
1091
1092 /**
1093  * Append a filename to a configuration value that
1094  * represents a list of filenames
1095  *
1096  * @param cfg configuration to update
1097  * @param section section of interest
1098  * @param option option of interest
1099  * @param value filename to append
1100  * @return GNUNET_OK on success,
1101  *         GNUNET_NO if the filename already in the list
1102  *         GNUNET_SYSERR on error
1103  */
1104 int
1105 GNUNET_CONFIGURATION_append_value_filename (struct GNUNET_CONFIGURATION_Handle
1106                                             *cfg, const char *section,
1107                                             const char *option,
1108                                             const char *value)
1109 {
1110   char *escaped;
1111   char *old;
1112   char *nw;
1113
1114   if (GNUNET_SYSERR ==
1115       GNUNET_CONFIGURATION_iterate_value_filenames (cfg, section, option,
1116                                                     &test_match,
1117                                                     (void *) value))
1118     return GNUNET_NO;           /* already exists */
1119   if (GNUNET_OK !=
1120       GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &old))
1121     old = GNUNET_strdup ("");
1122   escaped = escape_name (value);
1123   nw = GNUNET_malloc (strlen (old) + strlen (escaped) + 2);
1124   strcpy (nw, old);
1125   if (strlen (old) > 0)
1126     strcat (nw, " ");
1127   strcat (nw, escaped);
1128   GNUNET_CONFIGURATION_set_value_string (cfg, section, option, nw);
1129   GNUNET_free (old);
1130   GNUNET_free (nw);
1131   GNUNET_free (escaped);
1132   return GNUNET_OK;
1133 }
1134
1135
1136 /**
1137  * Remove a filename from a configuration value that
1138  * represents a list of filenames
1139  *
1140  * @param cfg configuration to update
1141  * @param section section of interest
1142  * @param option option of interest
1143  * @param value filename to remove
1144  * @return GNUNET_OK on success,
1145  *         GNUNET_NO if the filename is not in the list,
1146  *         GNUNET_SYSERR on error
1147  */
1148 int
1149 GNUNET_CONFIGURATION_remove_value_filename (struct GNUNET_CONFIGURATION_Handle
1150                                             *cfg, const char *section,
1151                                             const char *option,
1152                                             const char *value)
1153 {
1154   char *list;
1155   char *pos;
1156   char *end;
1157   char *match;
1158   char old;
1159
1160   if (GNUNET_OK !=
1161       GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &list))
1162     return GNUNET_NO;
1163   match = escape_name (value);
1164   pos = list;
1165   while (1)
1166   {
1167     while (pos[0] == ' ')
1168       pos++;
1169     if (strlen (pos) == 0)
1170       break;
1171     end = pos + 1;
1172     while ((end[0] != ' ') && (end[0] != '\0'))
1173     {
1174       if (end[0] == '\\')
1175       {
1176         switch (end[1])
1177         {
1178         case '\\':
1179         case ' ':
1180           end++;
1181           break;
1182         case '\0':
1183           /* illegal, but just keep it */
1184           break;
1185         default:
1186           /* illegal, but just ignore that there was a '/' */
1187           break;
1188         }
1189       }
1190       end++;
1191     }
1192     old = end[0];
1193     end[0] = '\0';
1194     if (0 == strcmp (pos, match))
1195     {
1196       if (old != '\0')
1197         memmove (pos, &end[1], strlen (&end[1]) + 1);
1198       else
1199       {
1200         if (pos != list)
1201           pos[-1] = '\0';
1202         else
1203           pos[0] = '\0';
1204       }
1205       GNUNET_CONFIGURATION_set_value_string (cfg, section, option, list);
1206       GNUNET_free (list);
1207       GNUNET_free (match);
1208       return GNUNET_OK;
1209     }
1210     if (old == '\0')
1211       break;
1212     end[0] = old;
1213     pos = end + 1;
1214   }
1215   GNUNET_free (list);
1216   GNUNET_free (match);
1217   return GNUNET_NO;
1218 }
1219
1220
1221 /**
1222  * Wrapper around GNUNET_CONFIGURATION_parse.
1223  *
1224  * @param cls the cfg
1225  * @param filename file to parse
1226  * @return GNUNET_OK on success
1227  */
1228 static int
1229 parse_configuration_file (void *cls, const char *filename)
1230 {
1231   struct GNUNET_CONFIGURATION_Handle *cfg = cls;
1232   char * ext;
1233   int ret;
1234
1235   /* Examine file extension */
1236   ext = strrchr (filename, '.');
1237   if ((NULL == ext) || (0 != strcmp (ext, ".conf")))
1238   {
1239     GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Skipping file `%s'\n", filename);
1240     return GNUNET_OK;
1241   }
1242
1243   ret = GNUNET_CONFIGURATION_parse (cfg, filename);
1244   return ret;
1245 }
1246
1247
1248 /**
1249  * Load default configuration.  This function will parse the
1250  * defaults from the given defaults_d directory.
1251  *
1252  * @param cfg configuration to update
1253  * @param defaults_d directory with the defaults
1254  * @return GNUNET_OK on success, GNUNET_SYSERR on error
1255  */
1256 int
1257 GNUNET_CONFIGURATION_load_from (struct GNUNET_CONFIGURATION_Handle *cfg,
1258                                 const char *defaults_d)
1259 {
1260   if (GNUNET_SYSERR ==
1261       GNUNET_DISK_directory_scan (defaults_d, &parse_configuration_file, cfg))
1262     return GNUNET_SYSERR;       /* no configuration at all found */
1263   return GNUNET_OK;
1264 }
1265
1266
1267 /**
1268  * Load configuration (starts with defaults, then loads
1269  * system-specific configuration).
1270  *
1271  * @param cfg configuration to update
1272  * @param filename name of the configuration file, NULL to load defaults
1273  * @return GNUNET_OK on success, GNUNET_SYSERR on error
1274  */
1275 int
1276 GNUNET_CONFIGURATION_load (struct GNUNET_CONFIGURATION_Handle *cfg,
1277                            const char *filename)
1278 {
1279   char *baseconfig;
1280   char *ipath;
1281
1282   ipath = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_DATADIR);
1283   if (ipath == NULL)
1284     return GNUNET_SYSERR;
1285   baseconfig = NULL;
1286   GNUNET_asprintf (&baseconfig, "%s%s", ipath, "config.d");
1287   GNUNET_free (ipath);
1288   if (GNUNET_SYSERR ==
1289       GNUNET_DISK_directory_scan (baseconfig, &parse_configuration_file, cfg))
1290   {
1291     GNUNET_free (baseconfig);
1292     return GNUNET_SYSERR;       /* no configuration at all found */
1293   }
1294   GNUNET_free (baseconfig);
1295   if ((filename != NULL) &&
1296       (GNUNET_OK != GNUNET_CONFIGURATION_parse (cfg, filename)))
1297   {
1298     /* specified configuration not found */
1299     return GNUNET_SYSERR;
1300   }
1301   if (((GNUNET_YES !=
1302         GNUNET_CONFIGURATION_have_value (cfg, "PATHS", "DEFAULTCONFIG"))) &&
1303       (filename != NULL))
1304     GNUNET_CONFIGURATION_set_value_string (cfg, "PATHS", "DEFAULTCONFIG",
1305                                            filename);
1306   if ((GNUNET_YES ==
1307        GNUNET_CONFIGURATION_have_value (cfg, "TESTING", "WEAKRANDOM")) &&
1308       (GNUNET_YES ==
1309        GNUNET_CONFIGURATION_get_value_yesno (cfg, "TESTING", "WEAKRANDOM")))
1310     GNUNET_CRYPTO_random_disable_entropy_gathering ();
1311   return GNUNET_OK;
1312 }
1313
1314
1315
1316 /* end of configuration.c */