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