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