indentation
[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
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     GNUNET_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       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
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   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     GNUNET_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, "\n"))
342     {
343       error = 1;
344       break;
345     }
346     sec = sec->next;
347   }
348   if (error != 0)
349     GNUNET_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 != strcmp (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 == strcmp (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
636   e = findEntry (cfg, section, option);
637   if (e != NULL)
638   {
639     GNUNET_free_non_null (e->val);
640     e->val = GNUNET_strdup (value);
641     return;
642   }
643   sec = findSection (cfg, section);
644   if (sec == NULL)
645   {
646     sec = GNUNET_malloc (sizeof (struct ConfigSection));
647     sec->name = GNUNET_strdup (section);
648     sec->next = cfg->sections;
649     cfg->sections = sec;
650   }
651   e = GNUNET_malloc (sizeof (struct ConfigEntry));
652   e->key = GNUNET_strdup (option);
653   e->val = GNUNET_strdup (value);
654   e->next = sec->entries;
655   sec->entries = e;
656 }
657
658
659 /**
660  * Set a configuration value that should be a number.
661  *
662  * @param cfg configuration to update
663  * @param section section of interest
664  * @param option option of interest
665  * @param number value to set
666  */
667 void
668 GNUNET_CONFIGURATION_set_value_number (struct GNUNET_CONFIGURATION_Handle *cfg,
669                                        const char *section, const char *option,
670                                        unsigned long long number)
671 {
672   char s[64];
673
674   GNUNET_snprintf (s, 64, "%llu", number);
675   GNUNET_CONFIGURATION_set_value_string (cfg, section, option, s);
676 }
677
678
679 /**
680  * Get a configuration value that should be a number.
681  *
682  * @param cfg configuration to inspect
683  * @param section section of interest
684  * @param option option of interest
685  * @param number where to store the numeric value of the option
686  * @return GNUNET_OK on success, GNUNET_SYSERR on error
687  */
688 int
689 GNUNET_CONFIGURATION_get_value_number (const struct GNUNET_CONFIGURATION_Handle
690                                        *cfg, const char *section,
691                                        const char *option,
692                                        unsigned long long *number)
693 {
694   struct ConfigEntry *e;
695
696   e = findEntry (cfg, section, option);
697   if (e == NULL)
698     return GNUNET_SYSERR;
699   if (1 != SSCANF (e->val, "%llu", number))
700     return GNUNET_SYSERR;
701   return GNUNET_OK;
702 }
703
704
705 /**
706  * Get a configuration value that should be a relative time.
707  *
708  * @param cfg configuration to inspect
709  * @param section section of interest
710  * @param option option of interest
711  * @param time set to the time value stored in the configuration
712  * @return GNUNET_OK on success, GNUNET_SYSERR on error
713  */
714 int
715 GNUNET_CONFIGURATION_get_value_time (const struct GNUNET_CONFIGURATION_Handle
716                                      *cfg, const char *section,
717                                      const char *option,
718                                      struct GNUNET_TIME_Relative *time)
719 {
720   struct ConfigEntry *e;
721   unsigned long long num;
722
723   e = findEntry (cfg, section, option);
724   if (e == NULL)
725     return GNUNET_SYSERR;
726   if ((0 == strcasecmp (e->val, "infinity")) ||
727       (0 == strcasecmp (e->val, "forever")))
728   {
729     *time = GNUNET_TIME_UNIT_FOREVER_REL;
730     return GNUNET_OK;
731   }
732   if (1 != SSCANF (e->val, "%llu", &num))
733     return GNUNET_SYSERR;
734   time->rel_value = (uint64_t) num;
735   return GNUNET_OK;
736 }
737
738
739 /**
740  * Get a configuration value that should be a string.
741  *
742  * @param cfg configuration to inspect
743  * @param section section of interest
744  * @param option option of interest
745  * @param value will be set to a freshly allocated configuration
746  *        value, or NULL if option is not specified
747  * @return GNUNET_OK on success, GNUNET_SYSERR on error
748  */
749 int
750 GNUNET_CONFIGURATION_get_value_string (const struct GNUNET_CONFIGURATION_Handle
751                                        *cfg, const char *section,
752                                        const char *option, char **value)
753 {
754   struct ConfigEntry *e;
755
756   e = findEntry (cfg, section, option);
757   if ((e == NULL) || (e->val == NULL))
758   {
759     *value = NULL;
760     return GNUNET_SYSERR;
761   }
762   *value = GNUNET_strdup (e->val);
763   return GNUNET_OK;
764 }
765
766
767 /**
768  * Get a configuration value that should be in a set of
769  * predefined strings
770  *
771  * @param cfg configuration to inspect
772  * @param section section of interest
773  * @param option option of interest
774  * @param choices NULL-terminated list of legal values
775  * @param value will be set to an entry in the legal list,
776  *        or NULL if option is not specified and no default given
777  * @return GNUNET_OK on success, GNUNET_SYSERR on error
778  */
779 int
780 GNUNET_CONFIGURATION_get_value_choice (const struct GNUNET_CONFIGURATION_Handle
781                                        *cfg, const char *section,
782                                        const char *option, const char **choices,
783                                        const char **value)
784 {
785   struct ConfigEntry *e;
786   int i;
787
788   e = findEntry (cfg, section, option);
789   if (e == NULL)
790     return GNUNET_SYSERR;
791   i = 0;
792   while (choices[i] != NULL)
793   {
794     if (0 == strcasecmp (choices[i], e->val))
795       break;
796     i++;
797   }
798   if (choices[i] == NULL)
799   {
800     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
801                 _("Configuration value '%s' for '%s'"
802                   " in section '%s' is not in set of legal choices\n"), e->val,
803                 option, section);
804     return GNUNET_SYSERR;
805   }
806   *value = choices[i];
807   return GNUNET_OK;
808 }
809
810
811 /**
812  * Test if we have a value for a particular option
813  * @param cfg configuration to inspect
814  * @param section section of interest
815  * @param option option of interest
816  * @return GNUNET_YES if so, GNUNET_NO if not.
817  */
818 int
819 GNUNET_CONFIGURATION_have_value (const struct GNUNET_CONFIGURATION_Handle *cfg,
820                                  const char *section, const char *option)
821 {
822   struct ConfigEntry *e;
823
824   if ((NULL == (e = findEntry (cfg, section, option))) || (e->val == NULL))
825     return GNUNET_NO;
826   return GNUNET_YES;
827 }
828
829
830 /**
831  * Expand an expression of the form "$FOO/BAR" to "DIRECTORY/BAR"
832  * where either in the "PATHS" section or the environtment
833  * "FOO" is set to "DIRECTORY".
834  *
835  * @param cfg configuration to use for path expansion
836  * @param orig string to $-expand (will be freed!)
837  * @return $-expanded string
838  */
839 char *
840 GNUNET_CONFIGURATION_expand_dollar (const struct GNUNET_CONFIGURATION_Handle
841                                     *cfg, char *orig)
842 {
843   int i;
844   char *prefix;
845   char *result;
846   const char *post;
847   const char *env;
848
849   if (orig[0] != '$')
850     return orig;
851   i = 0;
852   while ((orig[i] != '/') && (orig[i] != '\\') && (orig[i] != '\0'))
853     i++;
854   if (orig[i] == '\0')
855   {
856     post = "";
857   }
858   else
859   {
860     orig[i] = '\0';
861     post = &orig[i + 1];
862   }
863   if (GNUNET_OK !=
864       GNUNET_CONFIGURATION_get_value_filename (cfg, "PATHS", &orig[1], &prefix))
865   {
866     if (NULL == (env = getenv (&orig[1])))
867     {
868       orig[i] = DIR_SEPARATOR;
869       return orig;
870     }
871     prefix = GNUNET_strdup (env);
872   }
873   result = GNUNET_malloc (strlen (prefix) + strlen (post) + 2);
874   strcpy (result, prefix);
875   if ((strlen (prefix) == 0) ||
876       ((prefix[strlen (prefix) - 1] != DIR_SEPARATOR) && (strlen (post) > 0)))
877     strcat (result, DIR_SEPARATOR_STR);
878   strcat (result, post);
879   GNUNET_free (prefix);
880   GNUNET_free (orig);
881   return result;
882 }
883
884
885 /**
886  * Get a configuration value that should be a string.
887  *
888  * @param cfg configuration to inspect
889  * @param section section of interest
890  * @param option option of interest
891  * @param value will be set to a freshly allocated configuration
892  *        value, or NULL if option is not specified
893  * @return GNUNET_OK on success, GNUNET_SYSERR on error
894  */
895 int
896 GNUNET_CONFIGURATION_get_value_filename (const struct
897                                          GNUNET_CONFIGURATION_Handle *cfg,
898                                          const char *section,
899                                          const char *option, char **value)
900 {
901   char *tmp;
902
903   if (GNUNET_OK !=
904       GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &tmp))
905   {
906     *value = NULL;
907     return GNUNET_SYSERR;
908   }
909   tmp = GNUNET_CONFIGURATION_expand_dollar (cfg, tmp);
910   *value = GNUNET_STRINGS_filename_expand (tmp);
911   GNUNET_free (tmp);
912   if (*value == NULL)
913     return GNUNET_SYSERR;
914   return GNUNET_OK;
915 }
916
917
918 /**
919  * Get a configuration value that should be in a set of
920  * "GNUNET_YES" or "GNUNET_NO".
921  *
922  * @param cfg configuration to inspect
923  * @param section section of interest
924  * @param option option of interest
925  * @return GNUNET_YES, GNUNET_NO or GNUNET_SYSERR
926  */
927 int
928 GNUNET_CONFIGURATION_get_value_yesno (const struct GNUNET_CONFIGURATION_Handle
929                                       *cfg, const char *section,
930                                       const char *option)
931 {
932   static const char *yesno[] = { "YES", "NO", NULL };
933   const char *val;
934   int ret;
935
936   ret =
937       GNUNET_CONFIGURATION_get_value_choice (cfg, section, option, yesno, &val);
938   if (ret == GNUNET_SYSERR)
939     return ret;
940   if (val == yesno[0])
941     return GNUNET_YES;
942   return GNUNET_NO;
943 }
944
945
946 /**
947  * Iterate over the set of filenames stored in a configuration value.
948  *
949  * @param cfg configuration to inspect
950  * @param section section of interest
951  * @param option option of interest
952  * @param cb function to call on each filename
953  * @param cb_cls closure for cb
954  * @return number of filenames iterated over, -1 on error
955  */
956 int
957 GNUNET_CONFIGURATION_iterate_value_filenames (const struct
958                                               GNUNET_CONFIGURATION_Handle *cfg,
959                                               const char *section,
960                                               const char *option,
961                                               GNUNET_FileNameCallback cb,
962                                               void *cb_cls)
963 {
964   char *list;
965   char *pos;
966   char *end;
967   char old;
968   int ret;
969
970   if (GNUNET_OK !=
971       GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &list))
972     return 0;
973   GNUNET_assert (list != NULL);
974   ret = 0;
975   pos = list;
976   while (1)
977   {
978     while (pos[0] == ' ')
979       pos++;
980     if (strlen (pos) == 0)
981       break;
982     end = pos + 1;
983     while ((end[0] != ' ') && (end[0] != '\0'))
984     {
985       if (end[0] == '\\')
986       {
987         switch (end[1])
988         {
989         case '\\':
990         case ' ':
991           memmove (end, &end[1], strlen (&end[1]) + 1);
992         case '\0':
993           /* illegal, but just keep it */
994           break;
995         default:
996           /* illegal, but just ignore that there was a '/' */
997           break;
998         }
999       }
1000       end++;
1001     }
1002     old = end[0];
1003     end[0] = '\0';
1004     if (strlen (pos) > 0)
1005     {
1006       ret++;
1007       if ((cb != NULL) && (GNUNET_OK != cb (cb_cls, pos)))
1008       {
1009         ret = GNUNET_SYSERR;
1010         break;
1011       }
1012     }
1013     if (old == '\0')
1014       break;
1015     pos = end + 1;
1016   }
1017   GNUNET_free (list);
1018   return ret;
1019 }
1020
1021
1022 /**
1023  * FIXME.
1024  *
1025  * @param value FIXME
1026  * @return FIXME
1027  */
1028 static char *
1029 escape_name (const char *value)
1030 {
1031   char *escaped;
1032   const char *rpos;
1033   char *wpos;
1034
1035   escaped = GNUNET_malloc (strlen (value) * 2 + 1);
1036   memset (escaped, 0, strlen (value) * 2 + 1);
1037   rpos = value;
1038   wpos = escaped;
1039   while (rpos[0] != '\0')
1040   {
1041     switch (rpos[0])
1042     {
1043     case '\\':
1044     case ' ':
1045       wpos[0] = '\\';
1046       wpos[1] = rpos[0];
1047       wpos += 2;
1048       break;
1049     default:
1050       wpos[0] = rpos[0];
1051       wpos++;
1052     }
1053     rpos++;
1054   }
1055   return escaped;
1056 }
1057
1058
1059 /**
1060  * FIXME.
1061  *
1062  * @param cls string we compare with (const char*)
1063  * @param fn filename we are currently looking at
1064  * @return GNUNET_OK if the names do not match, GNUNET_SYSERR if they do
1065  */
1066 static int
1067 test_match (void *cls, const char *fn)
1068 {
1069   const char *of = cls;
1070
1071   return (0 == strcmp (of, fn)) ? GNUNET_SYSERR : GNUNET_OK;
1072 }
1073
1074
1075 /**
1076  * Append a filename to a configuration value that
1077  * represents a list of filenames
1078  *
1079  * @param cfg configuration to update
1080  * @param section section of interest
1081  * @param option option of interest
1082  * @param value filename to append
1083  * @return GNUNET_OK on success,
1084  *         GNUNET_NO if the filename already in the list
1085  *         GNUNET_SYSERR on error
1086  */
1087 int
1088 GNUNET_CONFIGURATION_append_value_filename (struct GNUNET_CONFIGURATION_Handle
1089                                             *cfg, const char *section,
1090                                             const char *option,
1091                                             const char *value)
1092 {
1093   char *escaped;
1094   char *old;
1095   char *nw;
1096
1097   if (GNUNET_SYSERR ==
1098       GNUNET_CONFIGURATION_iterate_value_filenames (cfg, section, option,
1099                                                     &test_match,
1100                                                     (void *) value))
1101     return GNUNET_NO;           /* already exists */
1102   if (GNUNET_OK !=
1103       GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &old))
1104     old = GNUNET_strdup ("");
1105   escaped = escape_name (value);
1106   nw = GNUNET_malloc (strlen (old) + strlen (escaped) + 2);
1107   strcpy (nw, old);
1108   if (strlen (old) > 0)
1109     strcat (nw, " ");
1110   strcat (nw, escaped);
1111   GNUNET_CONFIGURATION_set_value_string (cfg, section, option, nw);
1112   GNUNET_free (old);
1113   GNUNET_free (nw);
1114   GNUNET_free (escaped);
1115   return GNUNET_OK;
1116 }
1117
1118
1119 /**
1120  * Remove a filename from a configuration value that
1121  * represents a list of filenames
1122  *
1123  * @param cfg configuration to update
1124  * @param section section of interest
1125  * @param option option of interest
1126  * @param value filename to remove
1127  * @return GNUNET_OK on success,
1128  *         GNUNET_NO if the filename is not in the list,
1129  *         GNUNET_SYSERR on error
1130  */
1131 int
1132 GNUNET_CONFIGURATION_remove_value_filename (struct GNUNET_CONFIGURATION_Handle
1133                                             *cfg, const char *section,
1134                                             const char *option,
1135                                             const char *value)
1136 {
1137   char *list;
1138   char *pos;
1139   char *end;
1140   char *match;
1141   char old;
1142
1143   if (GNUNET_OK !=
1144       GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &list))
1145     return GNUNET_NO;
1146   match = escape_name (value);
1147   pos = list;
1148   while (1)
1149   {
1150     while (pos[0] == ' ')
1151       pos++;
1152     if (strlen (pos) == 0)
1153       break;
1154     end = pos + 1;
1155     while ((end[0] != ' ') && (end[0] != '\0'))
1156     {
1157       if (end[0] == '\\')
1158       {
1159         switch (end[1])
1160         {
1161         case '\\':
1162         case ' ':
1163           end++;
1164           break;
1165         case '\0':
1166           /* illegal, but just keep it */
1167           break;
1168         default:
1169           /* illegal, but just ignore that there was a '/' */
1170           break;
1171         }
1172       }
1173       end++;
1174     }
1175     old = end[0];
1176     end[0] = '\0';
1177     if (0 == strcmp (pos, match))
1178     {
1179       if (old != '\0')
1180         memmove (pos, &end[1], strlen (&end[1]) + 1);
1181       else
1182       {
1183         if (pos != list)
1184           pos[-1] = '\0';
1185         else
1186           pos[0] = '\0';
1187       }
1188       GNUNET_CONFIGURATION_set_value_string (cfg, section, option, list);
1189       GNUNET_free (list);
1190       GNUNET_free (match);
1191       return GNUNET_OK;
1192     }
1193     if (old == '\0')
1194       break;
1195     end[0] = old;
1196     pos = end + 1;
1197   }
1198   GNUNET_free (list);
1199   GNUNET_free (match);
1200   return GNUNET_NO;
1201 }
1202
1203
1204 /**
1205  * Load configuration (starts with defaults, then loads
1206  * system-specific configuration).
1207  *
1208  * @param cfg configuration to update
1209  * @param filename name of the configuration file, NULL to load defaults
1210  * @return GNUNET_OK on success, GNUNET_SYSERR on error
1211  */
1212 int
1213 GNUNET_CONFIGURATION_load (struct GNUNET_CONFIGURATION_Handle *cfg,
1214                            const char *filename)
1215 {
1216   char *baseconfig;
1217   char *ipath;
1218
1219   ipath = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_DATADIR);
1220   if (ipath == NULL)
1221     return GNUNET_SYSERR;
1222   baseconfig = NULL;
1223   GNUNET_asprintf (&baseconfig, "%s%s%s", ipath, DIR_SEPARATOR_STR,
1224                    "defaults.conf");
1225   GNUNET_free (ipath);
1226   if ((GNUNET_OK != GNUNET_CONFIGURATION_parse (cfg, baseconfig)) ||
1227       (!((filename == NULL) ||
1228          (GNUNET_OK == GNUNET_CONFIGURATION_parse (cfg, filename)))))
1229   {
1230     GNUNET_free (baseconfig);
1231     return (filename == NULL) ? GNUNET_OK : GNUNET_SYSERR;
1232   }
1233   GNUNET_free (baseconfig);
1234   if (((GNUNET_YES !=
1235         GNUNET_CONFIGURATION_have_value (cfg, "PATHS", "DEFAULTCONFIG"))) &&
1236       (filename != NULL))
1237     GNUNET_CONFIGURATION_set_value_string (cfg, "PATHS", "DEFAULTCONFIG",
1238                                            filename);
1239   if ((GNUNET_YES ==
1240        GNUNET_CONFIGURATION_have_value (cfg, "TESTING", "WEAKRANDOM")) &&
1241       (GNUNET_YES ==
1242        GNUNET_CONFIGURATION_get_value_yesno (cfg, "TESTING", "WEAKRANDOM")))
1243     GNUNET_CRYPTO_random_disable_entropy_gathering ();
1244   return GNUNET_OK;
1245 }
1246
1247
1248
1249 /* end of configuration.c */