some doxygen fixes
[oweals/gnunet.git] / src / util / configuration.c
1 /*
2      This file is part of GNUnet.
3      (C) 2006, 2007, 2008, 2009 Christian Grothoff (and other contributing authors)
4
5      GNUnet is free software; you can redistribute it and/or modify
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 2, or (at your
8      option) any later version.
9
10      GNUnet is distributed in the hope that it will be useful, but
11      WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20
21 /**
22  * @file src/util/configuration.c
23  * @brief configuration management
24  *
25  * @author Christian Grothoff
26  */
27
28 #include "platform.h"
29 #include "gnunet_common.h"
30 #include "gnunet_configuration_lib.h"
31 #include "gnunet_crypto_lib.h"
32 #include "gnunet_disk_lib.h"
33 #include "gnunet_os_lib.h"
34 #include "gnunet_strings_lib.h"
35
36
37 /**
38  * @brief configuration entry
39  */
40 struct ConfigEntry
41 {
42
43   /**
44    * This is a linked list.
45    */
46   struct ConfigEntry *next;
47
48   /**
49    * key for this entry
50    */
51   char *key;
52
53   /**
54    * current, commited value
55    */
56   char *val;
57 };
58
59
60 /**
61  * @brief configuration section
62  */
63 struct ConfigSection
64 {
65   /**
66    * This is a linked list.
67    */
68   struct ConfigSection *next;
69
70   /**
71    * entries in the section
72    */
73   struct ConfigEntry *entries;
74
75   /**
76    * name of the section
77    */
78   char *name;
79 };
80
81
82 /**
83  * @brief configuration data
84  */
85 struct GNUNET_CONFIGURATION_Handle
86 {
87   /**
88    * Configuration sections.
89    */
90   struct ConfigSection *sections;
91
92   /**
93    * Modification indication since last save
94    * GNUNET_NO if clean, GNUNET_YES if dirty,
95    * GNUNET_SYSERR on error (i.e. last save failed)
96    */
97   int dirty;
98
99 };
100
101
102 /**
103  * Create a GNUNET_CONFIGURATION_Handle.
104  *
105  * @return fresh configuration object
106  */
107 struct GNUNET_CONFIGURATION_Handle *
108 GNUNET_CONFIGURATION_create ()
109 {
110   return GNUNET_malloc (sizeof (struct GNUNET_CONFIGURATION_Handle));
111 }
112
113
114 /**
115  * Destroy configuration object.
116  *
117  * @param cfg configuration to destroy
118  */
119 void
120 GNUNET_CONFIGURATION_destroy (struct GNUNET_CONFIGURATION_Handle *cfg)
121 {
122   struct ConfigSection *sec;
123   struct ConfigEntry *ent;
124
125   while (NULL != (sec = cfg->sections))
126     {
127       cfg->sections = sec->next;
128       while (NULL != (ent = sec->entries))
129         {
130           sec->entries = ent->next;
131           GNUNET_free (ent->key);
132           GNUNET_free_non_null (ent->val);
133           GNUNET_free (ent);
134         }
135       GNUNET_free (sec->name);
136       GNUNET_free (sec);
137     }
138   GNUNET_free (cfg);
139 }
140
141
142 /**
143  * Parse a configuration file, add all of the options in the
144  * file to the configuration environment.
145  *
146  * @param cfg configuration to update
147  * @param filename name of the configuration file
148  * @return GNUNET_OK on success, GNUNET_SYSERR on error
149  */
150 int
151 GNUNET_CONFIGURATION_parse (struct GNUNET_CONFIGURATION_Handle *cfg,
152                             const char *filename)
153 {
154   int dirty;
155   char line[256];
156   char tag[64];
157   char value[192];
158   FILE *fp;
159   unsigned int nr;
160   int i;
161   int emptyline;
162   int ret;
163   char *section;
164   char *fn;
165
166   fn = GNUNET_STRINGS_filename_expand (filename);
167   dirty = cfg->dirty;           /* back up value! */
168   if (NULL == (fp = FOPEN (fn, "r")))
169     {
170       GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "fopen", fn);
171       GNUNET_free (fn);
172       return GNUNET_SYSERR;
173     }
174   GNUNET_free (fn);
175   ret = GNUNET_OK;
176   section = GNUNET_strdup ("");
177   memset (line, 0, 256);
178   nr = 0;
179   while (NULL != fgets (line, 255, fp))
180     {
181       nr++;
182       for (i = 0; i < 255; i++)
183         if (line[i] == '\t')
184           line[i] = ' ';
185       if (line[0] == '\n' || line[0] == '#' || line[0] == '%' ||
186           line[0] == '\r')
187         continue;
188       emptyline = 1;
189       for (i = 0; (i < 255 && line[i] != 0); i++)
190         if (line[i] != ' ' && line[i] != '\n' && line[i] != '\r')
191           emptyline = 0;
192       if (emptyline == 1)
193         continue;
194       /* remove tailing whitespace */
195       for (i = strlen (line) - 1; (i >= 0) && (isspace (line[i])); i--)
196         line[i] = '\0';
197       if (1 == sscanf (line, "@INLINE@ %191[^\n]", value))
198         {
199           /* @INLINE@ value */
200           if (0 != GNUNET_CONFIGURATION_parse (cfg, value))
201             ret = GNUNET_SYSERR;        /* failed to parse included config */
202         }
203       else if (1 == sscanf (line, "[%99[^]]]", value))
204         {
205           /* [value] */
206           GNUNET_free (section);
207           section = GNUNET_strdup (value);
208         }
209       else if (2 == sscanf (line, " %63[^= ] = %191[^\n]", tag, value))
210         {
211           /* tag = value */
212           /* Strip LF */
213           i = strlen (value) - 1;
214           while ((i >= 0) && (isspace (value[i])))
215             value[i--] = '\0';
216           /* remove quotes */
217           i = 0;
218           if (value[0] == '"')
219             {
220               i = 1;
221               while ((value[i] != '\0') && (value[i] != '"'))
222                 i++;
223               if (value[i] == '"')
224                 {
225                   value[i] = '\0';
226                   i = 1;
227                 }
228               else
229                 i = 0;
230             }
231           GNUNET_CONFIGURATION_set_value_string (cfg,
232                                                  section, tag, &value[i]);
233         }
234       else if (1 == sscanf (line, " %63[^= ] =[^\n]", tag))
235         {
236           /* tag = */
237           GNUNET_CONFIGURATION_set_value_string (cfg, section, tag, "");
238         }
239       else
240         {
241           /* parse error */
242           GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
243                       _
244                       ("Syntax error in configuration file `%s' at line %u.\n"),
245                       filename, nr);
246           ret = GNUNET_SYSERR;
247           break;
248         }
249     }
250   GNUNET_assert (0 == fclose (fp));
251   /* restore dirty flag - anything we set in the meantime
252      came from disk */
253   cfg->dirty = dirty;
254   GNUNET_free (section);
255   return ret;
256 }
257
258
259 /**
260  * Test if there are configuration options that were
261  * changed since the last save.
262  *
263  * @param cfg configuration to inspect
264  * @return GNUNET_NO if clean, GNUNET_YES if dirty, GNUNET_SYSERR on error (i.e. last save failed)
265  */
266 int
267 GNUNET_CONFIGURATION_is_dirty (const struct GNUNET_CONFIGURATION_Handle *cfg)
268 {
269   return cfg->dirty;
270 }
271
272
273 /**
274  * Write configuration file.
275  *
276  * @param cfg configuration to write
277  * @param filename where to write the configuration
278  * @return GNUNET_OK on success, GNUNET_SYSERR on error
279  */
280 int
281 GNUNET_CONFIGURATION_write (struct GNUNET_CONFIGURATION_Handle *data,
282                             const char *filename)
283 {
284   struct ConfigSection *sec;
285   struct ConfigEntry *ent;
286   FILE *fp;
287   int error;
288   char *fn;
289   char *val;
290   char *pos;
291
292   fn = GNUNET_STRINGS_filename_expand (filename);
293   GNUNET_DISK_directory_create_for_file (fn);
294   if (NULL == (fp = FOPEN (fn, "w")))
295     {
296       GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "fopen", fn);
297       GNUNET_free (fn);
298       return GNUNET_SYSERR;
299     }
300   GNUNET_free (fn);
301   error = 0;
302   sec = data->sections;
303   while (sec != NULL)
304     {
305       if (0 > fprintf (fp, "[%s]\n", sec->name))
306         {
307           error = 1;
308           break;
309         }
310       ent = sec->entries;
311       while (ent != NULL)
312         {
313           if (ent->val != NULL)
314             {
315               val = GNUNET_malloc (strlen (ent->val) * 2 + 1);
316               strcpy (val, ent->val);
317               while (NULL != (pos = strstr (val, "\n")))
318                 {
319                   memmove (&pos[2], &pos[1], strlen (&pos[1]));
320                   pos[0] = '\\';
321                   pos[1] = 'n';
322                 }
323               if (0 > fprintf (fp, "%s = %s\n", ent->key, val))
324                 {
325                   error = 1;
326                   GNUNET_free (val);
327                   break;
328                 }
329               GNUNET_free (val);
330             }
331           ent = ent->next;
332         }
333       if (error != 0)
334         break;
335       if (0 > fprintf (fp, "\n"))
336         {
337           error = 1;
338           break;
339         }
340       sec = sec->next;
341     }
342   if (error != 0)
343     GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "fprintf", filename);
344   GNUNET_assert (0 == fclose (fp));
345   if (error != 0)
346     {
347       data->dirty = GNUNET_SYSERR;      /* last write failed */
348       return GNUNET_SYSERR;
349     }
350   data->dirty = GNUNET_NO;      /* last write succeeded */
351   return GNUNET_OK;
352 }
353
354
355 /**
356  * Iterate over all options in the configuration.
357  *
358  * @param cfg configuration to inspect
359  * @param iter function to call on each option
360  * @param iter_cls closure for iter
361  */
362 void GNUNET_CONFIGURATION_iterate (const struct GNUNET_CONFIGURATION_Handle *cfg,
363                                    GNUNET_CONFIGURATION_Iterator iter,
364                                    void *iter_cls)
365 {
366   struct ConfigSection *spos;
367   struct ConfigEntry *epos;
368
369   spos = cfg->sections;
370   while (spos != NULL)
371     {
372       epos = spos->entries;
373       while (epos != NULL)
374         {
375           iter (iter_cls, spos->name, epos->key, epos->val);
376           epos = epos->next;
377         }
378       spos = spos->next;
379     }
380 }
381
382
383 /**
384  * FIXME.
385  *
386  * @param cls the destination configuration (struct GNUNET_CONFIGURATION_Handle*)
387  * @param section FIXME
388  * @param option FIXME
389  * @param value FIXME
390  */
391 static void
392 copy_entry (void *cls,
393             const char *section,
394             const char *option,
395             const char *value)
396 {
397   struct GNUNET_CONFIGURATION_Handle *dst = cls;
398   GNUNET_CONFIGURATION_set_value_string (dst, section, option, value);
399 }
400
401
402 /**
403  * Duplicate an existing configuration object.
404  *
405  * @param c configuration to duplicate
406  * @return duplicate configuration
407  */
408 struct GNUNET_CONFIGURATION_Handle *
409 GNUNET_CONFIGURATION_dup (const struct GNUNET_CONFIGURATION_Handle *cfg)
410 {
411   struct GNUNET_CONFIGURATION_Handle *ret;
412
413   ret = GNUNET_CONFIGURATION_create ();
414   GNUNET_CONFIGURATION_iterate (cfg, &copy_entry, ret);
415   return ret;
416 }
417
418
419 /**
420  * FIXME.
421  *
422  * @param data FIXME
423  * @param section FIXME
424  * @return matching entry, NULL if not found
425  */
426 static struct ConfigSection *
427 findSection (const struct GNUNET_CONFIGURATION_Handle *data, const char *section)
428 {
429   struct ConfigSection *pos;
430
431   pos = data->sections;
432   while ((pos != NULL) && (0 != strcasecmp (section, pos->name)))
433     pos = pos->next;
434   return pos;
435 }
436
437
438 /**
439  * FIXME.
440  *
441  * @param data FIXME
442  * @param section FIXME
443  * @param key FIXME
444  * @return matching entry, NULL if not found
445  */
446 static struct ConfigEntry *
447 findEntry (const struct GNUNET_CONFIGURATION_Handle *data,
448            const char *section, const char *key)
449 {
450   struct ConfigSection *sec;
451   struct ConfigEntry *pos;
452
453   sec = findSection (data, section);
454   if (sec == NULL)
455     return NULL;
456   pos = sec->entries;
457   while ((pos != NULL) && (0 != strcasecmp (key, pos->key)))
458     pos = pos->next;
459   return pos;
460 }
461
462
463 /**
464  * Set a configuration value that should be a string.
465  *
466  * @param cfg configuration to update
467  * @param section section of interest
468  * @param option option of interest
469  * @param value value to set
470  */
471 void
472 GNUNET_CONFIGURATION_set_value_string (struct GNUNET_CONFIGURATION_Handle
473                                        *data,
474                                        const char *section,
475                                        const char *option, const char *value)
476 {
477   struct ConfigSection *sec;
478   struct ConfigEntry *e;
479
480   e = findEntry (data, section, option);
481   if (e != NULL)
482     {
483       GNUNET_free_non_null (e->val);
484       e->val = GNUNET_strdup (value);
485       return;
486     }
487   sec = findSection (data, section);
488   if (sec == NULL)
489     {
490       sec = GNUNET_malloc (sizeof (struct ConfigSection));
491       sec->name = GNUNET_strdup (section);
492       sec->next = data->sections;
493       data->sections = sec;
494     }
495   e = GNUNET_malloc (sizeof (struct ConfigEntry));
496   e->key = GNUNET_strdup (option);
497   e->val = GNUNET_strdup (value);
498   e->next = sec->entries;
499   sec->entries = e;
500 }
501
502
503 /**
504  * Set a configuration value that should be a number.
505  *
506  * @param cfg configuration to update
507  * @param section section of interest
508  * @param option option of interest
509  * @param number value to set
510  */
511 void
512 GNUNET_CONFIGURATION_set_value_number (struct GNUNET_CONFIGURATION_Handle
513                                        *cfg, const char *section,
514                                        const char *option,
515                                        unsigned long long number)
516 {
517   char s[64];
518   GNUNET_snprintf (s, 64, "%llu", number);
519   GNUNET_CONFIGURATION_set_value_string (cfg, section, option, s);
520 }
521
522
523 /**
524  * Get a configuration value that should be a number.
525  *
526  * @param cfg configuration to inspect
527  * @param section section of interest
528  * @param option option of interest
529  * @param number where to store the numeric value of the option
530  * @return GNUNET_OK on success, GNUNET_SYSERR on error
531  */
532 int
533 GNUNET_CONFIGURATION_get_value_number (const struct GNUNET_CONFIGURATION_Handle
534                                        *cfg, const char *section,
535                                        const char *option,
536                                        unsigned long long *number)
537 {
538   struct ConfigEntry *e;
539
540   e = findEntry (cfg, section, option);
541   if (e == NULL)
542     return GNUNET_SYSERR;
543   if (1 != SSCANF (e->val, "%llu", number))
544     return GNUNET_SYSERR;
545   return GNUNET_OK;
546 }
547
548
549 /**
550  * Get a configuration value that should be a relative time.
551  *
552  * @param cfg configuration to inspect
553  * @param section section of interest
554  * @param option option of interest
555  * @param time set to the time value stored in the configuration
556  * @return GNUNET_OK on success, GNUNET_SYSERR on error
557  */
558 int
559 GNUNET_CONFIGURATION_get_value_time (const struct GNUNET_CONFIGURATION_Handle
560                                      *cfg, const char *section,
561                                      const char *option,
562                                      struct GNUNET_TIME_Relative *time)
563 {
564   unsigned long long num;
565   int ret;
566
567   ret = GNUNET_CONFIGURATION_get_value_number (cfg,
568                                                section,
569                                                option,
570                                                &num);
571   if (ret == GNUNET_OK)
572     time->value = (uint64_t) num;
573   return ret;
574 }
575
576
577 /**
578  * Get a configuration value that should be a string.
579  *
580  * @param cfg configuration to inspect
581  * @param section section of interest
582  * @param option option of interest
583  * @param value will be set to a freshly allocated configuration
584  *        value, or NULL if option is not specified
585  * @return GNUNET_OK on success, GNUNET_SYSERR on error
586  */
587 int
588 GNUNET_CONFIGURATION_get_value_string (const struct GNUNET_CONFIGURATION_Handle
589                                        *cfg, const char *section,
590                                        const char *option, char **value)
591 {
592   struct ConfigEntry *e;
593
594   e = findEntry (cfg, section, option);
595   if ((e == NULL) || (e->val == NULL))
596     {
597       *value = NULL;
598       return GNUNET_SYSERR;
599     }
600   *value = GNUNET_strdup (e->val);
601   return GNUNET_OK;
602 }
603
604
605 /**
606  * Get a configuration value that should be in a set of
607  * predefined strings
608  *
609  * @param cfg configuration to inspect
610  * @param section section of interest
611  * @param option option of interest
612  * @param choices NULL-terminated list of legal values
613  * @param value will be set to an entry in the legal list,
614  *        or NULL if option is not specified and no default given
615  * @return GNUNET_OK on success, GNUNET_SYSERR on error
616  */
617 int
618 GNUNET_CONFIGURATION_get_value_choice (const struct GNUNET_CONFIGURATION_Handle
619                                        *cfg, const char *section,
620                                        const char *option,
621                                        const char **choices,
622                                        const char **value)
623 {
624   struct ConfigEntry *e;
625   int i;
626
627   e = findEntry (cfg, section, option);
628   if (e == NULL)
629     return GNUNET_SYSERR;
630   i = 0;
631   while (choices[i] != NULL)
632     {
633       if (0 == strcasecmp (choices[i], e->val))
634         break;
635       i++;
636     }
637   if (choices[i] == NULL)
638     {
639       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
640                   _("Configuration value '%s' for '%s'"
641                     " in section '%s' is not in set of legal choices\n"),
642                   e->val, option, section);
643       return GNUNET_SYSERR;
644     }
645   *value = choices[i];
646   return GNUNET_OK;
647 }
648
649
650 /**
651  * Test if we have a value for a particular option
652  * @param cfg configuration to inspect
653  * @param section section of interest
654  * @param option option of interest
655  * @return GNUNET_YES if so, GNUNET_NO if not.
656  */
657 int
658 GNUNET_CONFIGURATION_have_value (const struct GNUNET_CONFIGURATION_Handle *cfg,
659                                  const char *section, const char *option)
660 {
661   struct ConfigEntry *e;
662   if ((NULL == (e = findEntry (cfg, section, option))) || (e->val == NULL))
663     return GNUNET_NO;
664   return GNUNET_YES;
665 }
666
667
668 /**
669  * Expand an expression of the form "$FOO/BAR" to "DIRECTORY/BAR"
670  * where either in the "PATHS" section or the environtment
671  * "FOO" is set to "DIRECTORY".
672  *
673  * @param cfg configuration to use for path expansion
674  * @param old string to $-expand (will be freed!)
675  * @return $-expanded string
676  */
677 char *
678 GNUNET_CONFIGURATION_expand_dollar (const struct GNUNET_CONFIGURATION_Handle *cfg,
679                                     char *orig)
680 {
681   int i;
682   char *prefix;
683   char *result;
684   const char *post;
685   const char *env;
686
687   if (orig[0] != '$')
688     return orig;
689   i = 0;
690   while ((orig[i] != '/') && (orig[i] != '\\') && (orig[i] != '\0'))
691     i++;
692   if (orig[i] == '\0')
693     {
694       post = "";
695     }
696   else
697     {
698       orig[i] = '\0';
699       post = &orig[i + 1];
700     }
701   if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (cfg,
702                                                           "PATHS",
703                                                           &orig[1], &prefix))
704     {
705       if (NULL == (env = getenv (&orig[1])))
706         {
707           orig[i] = DIR_SEPARATOR;
708           return orig;
709         }
710       prefix = GNUNET_strdup (env);
711     }
712   result = GNUNET_malloc (strlen (prefix) + strlen (post) + 2);
713   strcpy (result, prefix);
714   if ((strlen (prefix) == 0) ||
715       ((prefix[strlen (prefix) - 1] != DIR_SEPARATOR) && (strlen (post) > 0)))
716     strcat (result, DIR_SEPARATOR_STR);
717   strcat (result, post);
718   GNUNET_free (prefix);
719   GNUNET_free (orig);
720   return result;
721 }
722
723
724 /**
725  * Get a configuration value that should be a string.
726  *
727  * @param cfg configuration to inspect
728  * @param section section of interest
729  * @param option option of interest
730  * @param value will be set to a freshly allocated configuration
731  *        value, or NULL if option is not specified
732  * @return GNUNET_OK on success, GNUNET_SYSERR on error
733  */
734 int
735 GNUNET_CONFIGURATION_get_value_filename (const struct GNUNET_CONFIGURATION_Handle
736                                          *data, const char *section,
737                                          const char *option, char **value)
738 {
739   int ret;
740   char *tmp;
741
742   tmp = NULL;
743   ret = GNUNET_CONFIGURATION_get_value_string (data, section, option, &tmp);
744   if (ret == GNUNET_SYSERR)
745     return ret;
746   if (tmp != NULL)
747     {
748       tmp = GNUNET_CONFIGURATION_expand_dollar (data, tmp);
749       *value = GNUNET_STRINGS_filename_expand (tmp);
750       GNUNET_free (tmp);
751       if (*value == NULL)
752         ret = GNUNET_SYSERR;
753     }
754   else
755     {
756       *value = NULL;
757     }
758   return ret;
759 }
760
761
762 /**
763  * Get a configuration value that should be in a set of
764  * "GNUNET_YES" or "GNUNET_NO".
765  *
766  * @param cfg configuration to inspect
767  * @param section section of interest
768  * @param option option of interest
769  * @return GNUNET_YES, GNUNET_NO or GNUNET_SYSERR
770  */
771 int
772 GNUNET_CONFIGURATION_get_value_yesno (const struct GNUNET_CONFIGURATION_Handle *cfg,
773                                       const char *section, const char *option)
774 {
775   static const char *yesno[] = { "YES", "NO", NULL };
776   const char *val;
777   int ret;
778
779   ret = GNUNET_CONFIGURATION_get_value_choice (cfg,
780                                                section, option, yesno, &val);
781   if (ret == GNUNET_SYSERR)
782     return ret;
783   if (val == yesno[0])
784     return GNUNET_YES;
785   return GNUNET_NO;
786 }
787
788
789 /**
790  * Iterate over the set of filenames stored in a configuration value.
791  *
792  * @param cfg configuration to inspect
793  * @param section section of interest
794  * @param option option of interest
795  * @param cb function to call on each filename
796  * @param cb_cls closure for cb
797  * @return number of filenames iterated over, -1 on error
798  */
799 int
800 GNUNET_CONFIGURATION_iterate_value_filenames (const struct
801                                               GNUNET_CONFIGURATION_Handle
802                                               *cfg, const char *section,
803                                               const char *option,
804                                               GNUNET_FileNameCallback cb,
805                                               void *cb_cls)
806 {
807   char *list;
808   char *pos;
809   char *end;
810   char old;
811   int ret;
812
813   if (GNUNET_OK !=
814       GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &list))
815     return 0;
816   GNUNET_assert (list != NULL);
817   ret = 0;
818   pos = list;
819   while (1)
820     {
821       while (pos[0] == ' ')
822         pos++;
823       if (strlen (pos) == 0)
824         break;
825       end = pos + 1;
826       while ((end[0] != ' ') && (end[0] != '\0'))
827         {
828           if (end[0] == '\\')
829             {
830               switch (end[1])
831                 {
832                 case '\\':
833                 case ' ':
834                   memmove (end, &end[1], strlen (&end[1]) + 1);
835                 case '\0':
836                   /* illegal, but just keep it */
837                   break;
838                 default:
839                   /* illegal, but just ignore that there was a '/' */
840                   break;
841                 }
842             }
843           end++;
844         }
845       old = end[0];
846       end[0] = '\0';
847       if (strlen (pos) > 0)
848         {
849           ret++;
850           if ((cb != NULL) && (GNUNET_OK != cb (cb_cls, pos)))
851             {
852               ret = GNUNET_SYSERR;
853               break;
854             }
855         }
856       if (old == '\0')
857         break;
858       pos = end + 1;
859     }
860   GNUNET_free (list);
861   return ret;
862 }
863
864
865 /**
866  * FIXME.
867  *
868  * @param value FIXME
869  * @return FIXME
870  */
871 static char *
872 escape_name (const char *value)
873 {
874   char *escaped;
875   const char *rpos;
876   char *wpos;
877
878   escaped = GNUNET_malloc (strlen (value) * 2 + 1);
879   memset (escaped, 0, strlen (value) * 2 + 1);
880   rpos = value;
881   wpos = escaped;
882   while (rpos[0] != '\0')
883     {
884       switch (rpos[0])
885         {
886         case '\\':
887         case ' ':
888           wpos[0] = '\\';
889           wpos[1] = rpos[0];
890           wpos += 2;
891           break;
892         default:
893           wpos[0] = rpos[0];
894           wpos++;
895         }
896       rpos++;
897     }
898   return escaped;
899 }
900
901
902 /**
903  * FIXME.
904  *
905  * @param cls string we compare with (const char*)
906  * @param fn filename we are currently looking at
907  * @return GNUNET_OK if the names do not match, GNUNET_SYSERR if they do
908  */
909 static int
910 test_match (void *cls, const char *fn)
911 {
912   const char *of = cls;
913   return (0 == strcmp (of, fn)) ? GNUNET_SYSERR : GNUNET_OK;
914 }
915
916
917 /**
918  * Append a filename to a configuration value that
919  * represents a list of filenames
920  *
921  * @param cfg configuration to update
922  * @param section section of interest
923  * @param option option of interest
924  * @param value filename to append
925  * @return GNUNET_OK on success,
926  *         GNUNET_NO if the filename already in the list
927  *         GNUNET_SYSERR on error
928  */
929 int
930 GNUNET_CONFIGURATION_append_value_filename (struct GNUNET_CONFIGURATION_Handle
931                                             *cfg,
932                                             const char *section,
933                                             const char *option,
934                                             const char *value)
935 {
936   char *escaped;
937   char *old;
938   char *nw;
939
940   if (GNUNET_SYSERR
941       == GNUNET_CONFIGURATION_iterate_value_filenames (cfg,
942                                                        section,
943                                                        option,
944                                                        &test_match,
945                                                        (void *) value))
946     return GNUNET_NO;           /* already exists */
947   if (GNUNET_OK !=
948       GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &old))
949     old = GNUNET_strdup ("");
950   escaped = escape_name (value);
951   nw = GNUNET_malloc (strlen (old) + strlen (escaped) + 2);
952   strcpy (nw, old);
953   strcat (nw, " ");
954   strcat (nw, escaped);
955   GNUNET_CONFIGURATION_set_value_string (cfg, section, option, nw);
956   GNUNET_free (old);
957   GNUNET_free (nw);
958   GNUNET_free (escaped);
959   return GNUNET_OK;
960 }
961
962
963 /**
964  * Remove a filename from a configuration value that
965  * represents a list of filenames
966  *
967  * @param cfg configuration to update
968  * @param section section of interest
969  * @param option option of interest
970  * @param value filename to remove
971  * @return GNUNET_OK on success,
972  *         GNUNET_NO if the filename is not in the list,
973  *         GNUNET_SYSERR on error
974  */
975 int
976 GNUNET_CONFIGURATION_remove_value_filename (struct GNUNET_CONFIGURATION_Handle
977                                             *cfg,
978                                             const char *section,
979                                             const char *option,
980                                             const char *value)
981 {
982   char *list;
983   char *pos;
984   char *end;
985   char *match;
986   char old;
987
988   if (GNUNET_OK !=
989       GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &list))
990     return GNUNET_NO;
991   match = escape_name (value);
992   pos = list;
993   while (1)
994     {
995       while (pos[0] == ' ')
996         pos++;
997       if (strlen (pos) == 0)
998         break;
999       end = pos + 1;
1000       while ((end[0] != ' ') && (end[0] != '\0'))
1001         {
1002           if (end[0] == '\\')
1003             {
1004               switch (end[1])
1005                 {
1006                 case '\\':
1007                 case ' ':
1008                   end++;
1009                   break;
1010                 case '\0':
1011                   /* illegal, but just keep it */
1012                   break;
1013                 default:
1014                   /* illegal, but just ignore that there was a '/' */
1015                   break;
1016                 }
1017             }
1018           end++;
1019         }
1020       old = end[0];
1021       end[0] = '\0';
1022       if (strlen (pos) > 0)
1023         {
1024           if (0 == strcmp (pos, match))
1025             {
1026               memmove (pos, &end[1], strlen (&end[1]) + 1);
1027
1028               if (pos != list)
1029                 pos[-1] = ' ';  /* previously changed to "\0" */
1030               GNUNET_CONFIGURATION_set_value_string (cfg,
1031                                                      section, option, list);
1032               GNUNET_free (list);
1033               GNUNET_free (match);
1034               return GNUNET_OK;
1035             }
1036         }
1037       if (old == '\0')
1038         break;
1039       pos = end + 1;
1040     }
1041   GNUNET_free (list);
1042   GNUNET_free (match);
1043   return GNUNET_NO;
1044 }
1045
1046
1047 /**
1048  * Load configuration (starts with defaults, then loads
1049  * system-specific configuration).
1050  *
1051  * @param cfg configuration to update
1052  * @param filename name of the configuration file
1053  * @return GNUNET_OK on success, GNUNET_SYSERR on error
1054  */
1055 int
1056 GNUNET_CONFIGURATION_load (struct GNUNET_CONFIGURATION_Handle *cfg,
1057                            const char *cfgfn)
1058 {
1059   char *baseconfig;
1060   char *ipath;
1061
1062   ipath = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_DATADIR);
1063   if (ipath == NULL)
1064     return GNUNET_SYSERR;
1065   baseconfig = NULL;
1066   GNUNET_asprintf (&baseconfig,
1067                    "%s%s%s", ipath, DIR_SEPARATOR_STR, "defaults.conf");
1068   GNUNET_free (ipath);
1069   if ((GNUNET_OK !=
1070        GNUNET_CONFIGURATION_parse (cfg, baseconfig)) ||
1071       (!((cfgfn == NULL) ||
1072          (GNUNET_OK == GNUNET_CONFIGURATION_parse (cfg, cfgfn)))))
1073     {
1074       GNUNET_free (baseconfig);
1075       return GNUNET_SYSERR;
1076     }
1077   GNUNET_free (baseconfig);
1078   if ((GNUNET_YES == GNUNET_CONFIGURATION_have_value (cfg,
1079                                                       "TESTING",
1080                                                       "WEAKRANDOM")) &&
1081       (GNUNET_YES == GNUNET_CONFIGURATION_get_value_yesno (cfg,
1082                                                            "TESTING",
1083                                                            "WEAKRANDOM")))
1084     GNUNET_CRYPTO_random_disable_entropy_gathering ();
1085   return GNUNET_OK;
1086 }
1087
1088
1089
1090 /* end of configuration.c */