gnunet_configuration_lib.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. /*
  2. This file is part of GNUnet.
  3. (C) 2006, 2008, 2009 Christian Grothoff (and other contributing authors)
  4. GNUnet is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published
  6. by the Free Software Foundation; either version 2, or (at your
  7. option) any later version.
  8. GNUnet is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNUnet; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  15. Boston, MA 02111-1307, USA.
  16. */
  17. /**
  18. * @file include/gnunet_configuration_lib.h
  19. * @brief configuration API
  20. *
  21. * @author Christian Grothoff
  22. */
  23. #ifndef GNUNET_CONFIGURATION_LIB_H
  24. #define GNUNET_CONFIGURATION_LIB_H
  25. #ifdef __cplusplus
  26. extern "C"
  27. {
  28. #if 0 /* keep Emacsens' auto-indent happy */
  29. }
  30. #endif
  31. #endif
  32. #include "gnunet_common.h"
  33. #include "gnunet_time_lib.h"
  34. /**
  35. * A configuration object.
  36. */
  37. struct GNUNET_CONFIGURATION_Handle;
  38. /**
  39. * Create a new configuration object.
  40. * @return fresh configuration object
  41. */
  42. struct GNUNET_CONFIGURATION_Handle *
  43. GNUNET_CONFIGURATION_create (void);
  44. /**
  45. * Duplicate an existing configuration object.
  46. *
  47. * @param cfg configuration to duplicate
  48. * @return duplicate configuration
  49. */
  50. struct GNUNET_CONFIGURATION_Handle *
  51. GNUNET_CONFIGURATION_dup (const struct GNUNET_CONFIGURATION_Handle *cfg);
  52. /**
  53. * Destroy configuration object.
  54. *
  55. * @param cfg configuration to destroy
  56. */
  57. void
  58. GNUNET_CONFIGURATION_destroy (struct GNUNET_CONFIGURATION_Handle *cfg);
  59. /**
  60. * Load configuration. This function will first parse the
  61. * defaults and then parse the specific configuration file
  62. * to overwrite the defaults.
  63. *
  64. * @param cfg configuration to update
  65. * @param filename name of the configuration file, NULL to load defaults
  66. * @return GNUNET_OK on success, GNUNET_SYSERR on error
  67. */
  68. int
  69. GNUNET_CONFIGURATION_load (struct GNUNET_CONFIGURATION_Handle *cfg,
  70. const char *filename);
  71. /**
  72. * Parse a configuration file, add all of the options in the
  73. * file to the configuration environment.
  74. *
  75. * @param cfg configuration to update
  76. * @param filename name of the configuration file
  77. * @return GNUNET_OK on success, GNUNET_SYSERR on error
  78. */
  79. int
  80. GNUNET_CONFIGURATION_parse (struct GNUNET_CONFIGURATION_Handle *cfg,
  81. const char *filename);
  82. /**
  83. * Write configuration file.
  84. *
  85. * @param cfg configuration to write
  86. * @param filename where to write the configuration
  87. * @return GNUNET_OK on success, GNUNET_SYSERR on error
  88. */
  89. int
  90. GNUNET_CONFIGURATION_write (struct GNUNET_CONFIGURATION_Handle *cfg,
  91. const char *filename);
  92. /**
  93. * Write only configuration entries that have been changed to configuration file
  94. * @param cfgDefault default configuration
  95. * @param cfgNew new configuration
  96. * @param filename where to write the configuration diff between default and new
  97. * @return GNUNET_OK on success, GNUNET_SYSERR on error
  98. */
  99. int
  100. GNUNET_CONFIGURATION_write_diffs (const struct GNUNET_CONFIGURATION_Handle
  101. *cfgDefault,
  102. const struct GNUNET_CONFIGURATION_Handle
  103. *cfgNew, const char *filename);
  104. /**
  105. * Test if there are configuration options that were
  106. * changed since the last save.
  107. *
  108. * @param cfg configuration to inspect
  109. * @return GNUNET_NO if clean, GNUNET_YES if dirty, GNUNET_SYSERR on error (i.e. last save failed)
  110. */
  111. int
  112. GNUNET_CONFIGURATION_is_dirty (const struct GNUNET_CONFIGURATION_Handle *cfg);
  113. /**
  114. * Function to iterate over options.
  115. *
  116. * @param cls closure
  117. * @param section name of the section
  118. * @param option name of the option
  119. * @param value value of the option
  120. */
  121. typedef void (*GNUNET_CONFIGURATION_Iterator) (void *cls, const char *section,
  122. const char *option,
  123. const char *value);
  124. /**
  125. * Function to iterate over section.
  126. *
  127. * @param cls closure
  128. * @param section name of the section
  129. */
  130. typedef void (*GNUNET_CONFIGURATION_Section_Iterator) (void *cls,
  131. const char *section);
  132. /**
  133. * Iterate over all options in the configuration.
  134. *
  135. * @param cfg configuration to inspect
  136. * @param iter function to call on each option
  137. * @param iter_cls closure for iter
  138. */
  139. void
  140. GNUNET_CONFIGURATION_iterate (const struct GNUNET_CONFIGURATION_Handle *cfg,
  141. GNUNET_CONFIGURATION_Iterator iter,
  142. void *iter_cls);
  143. /**
  144. * Iterate over all sections in the configuration.
  145. *
  146. * @param cfg configuration to inspect
  147. * @param iter function to call on each section
  148. * @param iter_cls closure for iter
  149. */
  150. void
  151. GNUNET_CONFIGURATION_iterate_sections (const struct GNUNET_CONFIGURATION_Handle
  152. *cfg,
  153. GNUNET_CONFIGURATION_Section_Iterator
  154. iter, void *iter_cls);
  155. /**
  156. * Remove the given section and all options in it.
  157. *
  158. * @param cfg configuration to inspect
  159. * @param section name of the section to remove
  160. */
  161. void
  162. GNUNET_CONFIGURATION_remove_section (struct GNUNET_CONFIGURATION_Handle *cfg,
  163. const char *section);
  164. /**
  165. * Get a configuration value that should be a number.
  166. *
  167. * @param cfg configuration to inspect
  168. * @param section section of interest
  169. * @param option option of interest
  170. * @param number where to store the numeric value of the option
  171. * @return GNUNET_OK on success, GNUNET_SYSERR on error
  172. */
  173. int
  174. GNUNET_CONFIGURATION_get_value_number (const struct GNUNET_CONFIGURATION_Handle
  175. *cfg, const char *section,
  176. const char *option,
  177. unsigned long long *number);
  178. /**
  179. * Get a configuration value that should be a relative time.
  180. *
  181. * @param cfg configuration to inspect
  182. * @param section section of interest
  183. * @param option option of interest
  184. * @param time set to the time value stored in the configuration
  185. * @return GNUNET_OK on success, GNUNET_SYSERR on error
  186. */
  187. int
  188. GNUNET_CONFIGURATION_get_value_time (const struct GNUNET_CONFIGURATION_Handle
  189. *cfg, const char *section,
  190. const char *option,
  191. struct GNUNET_TIME_Relative *time);
  192. /**
  193. * Test if we have a value for a particular option
  194. *
  195. * @param cfg configuration to inspect
  196. * @param section section of interest
  197. * @param option option of interest
  198. * @return GNUNET_YES if so, GNUNET_NO if not.
  199. */
  200. int
  201. GNUNET_CONFIGURATION_have_value (const struct GNUNET_CONFIGURATION_Handle *cfg,
  202. const char *section, const char *option);
  203. /**
  204. * Get a configuration value that should be a string.
  205. *
  206. * @param cfg configuration to inspect
  207. * @param section section of interest
  208. * @param option option of interest
  209. * @param value will be set to a freshly allocated configuration
  210. * value, or NULL if option is not specified
  211. * @return GNUNET_OK on success, GNUNET_SYSERR on error
  212. */
  213. int
  214. GNUNET_CONFIGURATION_get_value_string (const struct GNUNET_CONFIGURATION_Handle
  215. *cfg, const char *section,
  216. const char *option, char **value);
  217. /**
  218. * Get a configuration value that should be the name of a file
  219. * or directory.
  220. *
  221. * @param cfg configuration to inspect
  222. * @param section section of interest
  223. * @param option option of interest
  224. * @param value will be set to a freshly allocated configuration
  225. * value, or NULL if option is not specified
  226. * @return GNUNET_OK on success, GNUNET_SYSERR on error
  227. */
  228. int
  229. GNUNET_CONFIGURATION_get_value_filename (const struct
  230. GNUNET_CONFIGURATION_Handle *cfg,
  231. const char *section,
  232. const char *option, char **value);
  233. /**
  234. * Iterate over the set of filenames stored in a configuration value.
  235. *
  236. * @param cfg configuration to inspect
  237. * @param section section of interest
  238. * @param option option of interest
  239. * @param cb function to call on each filename
  240. * @param cb_cls closure for cb
  241. * @return number of filenames iterated over, -1 on error
  242. */
  243. int
  244. GNUNET_CONFIGURATION_iterate_value_filenames (const struct
  245. GNUNET_CONFIGURATION_Handle *cfg,
  246. const char *section,
  247. const char *option,
  248. GNUNET_FileNameCallback cb,
  249. void *cb_cls);
  250. /**
  251. * Iterate over values of a section in the configuration.
  252. *
  253. * @param cfg configuration to inspect
  254. * @param section the section
  255. * @param iter function to call on each option
  256. * @param iter_cls closure for iter
  257. */
  258. void
  259. GNUNET_CONFIGURATION_iterate_section_values (const struct
  260. GNUNET_CONFIGURATION_Handle *cfg,
  261. const char *section,
  262. GNUNET_CONFIGURATION_Iterator iter,
  263. void *iter_cls);
  264. /**
  265. * Get a configuration value that should be in a set of
  266. * predefined strings
  267. *
  268. * @param cfg configuration to inspect
  269. * @param section section of interest
  270. * @param option option of interest
  271. * @param choices NULL-terminated list of legal values
  272. * @param value will be set to an entry in the legal list,
  273. * or NULL if option is not specified and no default given
  274. * @return GNUNET_OK on success, GNUNET_SYSERR on error
  275. */
  276. int
  277. GNUNET_CONFIGURATION_get_value_choice (const struct GNUNET_CONFIGURATION_Handle
  278. *cfg, const char *section,
  279. const char *option, const char **choices,
  280. const char **value);
  281. /**
  282. * Get a configuration value that should be in a set of
  283. * "YES" or "NO".
  284. *
  285. * @param cfg configuration to inspect
  286. * @param section section of interest
  287. * @param option option of interest
  288. * @return GNUNET_YES, GNUNET_NO or if option has no valid value, GNUNET_SYSERR
  289. */
  290. int
  291. GNUNET_CONFIGURATION_get_value_yesno (const struct GNUNET_CONFIGURATION_Handle
  292. *cfg, const char *section,
  293. const char *option);
  294. /**
  295. * Expand an expression of the form "$FOO/BAR" to "DIRECTORY/BAR"
  296. * where either in the "PATHS" section or the environtment
  297. * "FOO" is set to "DIRECTORY".
  298. *
  299. * @param cfg configuration to use for path expansion
  300. * @param orig string to $-expand (will be freed!)
  301. * @return $-expanded string
  302. */
  303. char *
  304. GNUNET_CONFIGURATION_expand_dollar (const struct GNUNET_CONFIGURATION_Handle
  305. *cfg, char *orig);
  306. /**
  307. * Set a configuration value that should be a number.
  308. *
  309. * @param cfg configuration to update
  310. * @param section section of interest
  311. * @param option option of interest
  312. * @param number value to set
  313. */
  314. void
  315. GNUNET_CONFIGURATION_set_value_number (struct GNUNET_CONFIGURATION_Handle *cfg,
  316. const char *section, const char *option,
  317. unsigned long long number);
  318. /**
  319. * Set a configuration value that should be a string.
  320. *
  321. * @param cfg configuration to update
  322. * @param section section of interest
  323. * @param option option of interest
  324. * @param value value to set
  325. */
  326. void
  327. GNUNET_CONFIGURATION_set_value_string (struct GNUNET_CONFIGURATION_Handle *cfg,
  328. const char *section, const char *option,
  329. const char *value);
  330. /**
  331. * Remove a filename from a configuration value that
  332. * represents a list of filenames
  333. *
  334. * @param cfg configuration to update
  335. * @param section section of interest
  336. * @param option option of interest
  337. * @param value filename to remove
  338. * @return GNUNET_OK on success,
  339. * GNUNET_SYSERR if the filename is not in the list
  340. */
  341. int
  342. GNUNET_CONFIGURATION_remove_value_filename (struct GNUNET_CONFIGURATION_Handle
  343. *cfg, const char *section,
  344. const char *option,
  345. const char *value);
  346. /**
  347. * Append a filename to a configuration value that
  348. * represents a list of filenames
  349. *
  350. * @param cfg configuration to update
  351. * @param section section of interest
  352. * @param option option of interest
  353. * @param value filename to append
  354. * @return GNUNET_OK on success,
  355. * GNUNET_SYSERR if the filename already in the list
  356. */
  357. int
  358. GNUNET_CONFIGURATION_append_value_filename (struct GNUNET_CONFIGURATION_Handle
  359. *cfg, const char *section,
  360. const char *option,
  361. const char *value);
  362. #if 0 /* keep Emacsens' auto-indent happy */
  363. {
  364. #endif
  365. #ifdef __cplusplus
  366. }
  367. #endif
  368. #endif