environment.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. #include "environment.h"
  2. #include "data/bootstrap.h"
  3. /** @brief Default context.
  4. *
  5. * The actual default context IRI used throughout the application may use a
  6. * value from the `LSUP_DEFAULT_CTX_URI` environment variable.
  7. */
  8. #define DEFAULT_CTX_LABEL LSUP_NS "default"
  9. /*
  10. * External variables.
  11. */
  12. LSUP_NSMap *LSUP_default_nsm = NULL;
  13. LSUP_Term *LSUP_default_ctx = NULL;
  14. LSUP_Buffer *LSUP_default_ctx_buf = NULL;
  15. /*
  16. * Hashmap callbacks.
  17. */
  18. static uint64_t
  19. term_cache_hash_fn (
  20. const void *item, uint64_t seed0, uint64_t seed1)
  21. { return ((const LSUP_KeyedTerm *) item)->key; }
  22. static int
  23. term_cache_cmp_fn (const void *a, const void *b, void *udata)
  24. {
  25. return
  26. ((const LSUP_KeyedTerm *) a)->key -
  27. ((const LSUP_KeyedTerm *) b)->key;
  28. }
  29. static void
  30. term_cache_free_fn (void *item)
  31. { LSUP_term_free (((LSUP_KeyedTerm *) item)->term); }
  32. /*
  33. * API
  34. */
  35. LSUP_rc
  36. LSUP_init (void)
  37. {
  38. if (LSUP_IS_INIT) {
  39. LOG_RC (LSUP_NOACTION);
  40. return LSUP_NOACTION;
  41. }
  42. #ifdef DEBUG
  43. // In debug mode, always use max logging.
  44. int loglevel = LOG_TRACE;
  45. #else
  46. char *_loglevel = getenv ("LSUP_LOGLEVEL");
  47. int loglevel = (_loglevel == NULL) ? LOG_INFO : atoi (_loglevel);
  48. #endif
  49. log_set_level (loglevel);
  50. // URI validation pattern.
  51. MALLOC_GUARD (LSUP_uri_ptn, LSUP_MEM_ERR);
  52. #if 0 // Re-activate in case a change in the URI regex results in an error.
  53. int regex_rc = regcomp (LSUP_uri_ptn, LSUP_URI_REGEX_STR, REG_EXTENDED);
  54. if (regex_rc != 0) {
  55. char err_msg[128];
  56. size_t err_msg_sz = regerror (regex_rc, LSUP_uri_ptn, err_msg, 128);
  57. log_error (
  58. "Error compiling regular expression pattern: %s.",
  59. err_msg);
  60. return LSUP_ERROR;
  61. }
  62. #else
  63. if (regcomp (LSUP_uri_ptn, LSUP_URI_REGEX_STR, REG_EXTENDED) != 0) {
  64. log_error ("Error compiling regular expression pattern.");
  65. return LSUP_ERROR;
  66. }
  67. #endif
  68. // Default namespace map.
  69. LSUP_default_nsm = LSUP_nsmap_new();
  70. if (UNLIKELY (!LSUP_default_nsm)) return LSUP_ERROR;
  71. for (int i = 0; init_nsmap[i][0] != NULL; i++)
  72. LSUP_nsmap_add (LSUP_default_nsm, init_nsmap[i][0], init_nsmap[i][1]);
  73. // Default context URI.
  74. char *default_ctx_str = getenv ("LSUP_DEFAULT_CTX");
  75. if (!default_ctx_str ) default_ctx_str = DEFAULT_CTX_LABEL;
  76. LSUP_default_ctx = LSUP_iriref_new (default_ctx_str, LSUP_default_nsm);
  77. if (UNLIKELY (!LSUP_default_ctx)) return LSUP_ERROR;
  78. LSUP_default_ctx_buf = LSUP_term_serialize (LSUP_default_ctx);
  79. if (UNLIKELY (!LSUP_default_ctx_buf)) return LSUP_ERROR;
  80. // Initialize term cache.
  81. LSUP_term_cache = hashmap_new (
  82. sizeof(LSUP_KeyedTerm), 0, LSUP_HASH_SEED, 0,
  83. term_cache_hash_fn, term_cache_cmp_fn, term_cache_free_fn, NULL);
  84. // Create and cache default literal datatype key.
  85. LSUP_default_datatype = LSUP_iriref_new (DEFAULT_DTYPE, NULL);
  86. uint32_t dtype_hash = LSUP_term_hash (LSUP_default_datatype );
  87. LSUP_tcache_add (dtype_hash, LSUP_default_datatype);
  88. // Set automatic teardown TODO Is this a good idea?
  89. atexit (LSUP_done);
  90. return LSUP_OK;
  91. }
  92. void
  93. LSUP_done (void)
  94. {
  95. if (!LSUP_IS_INIT) return;
  96. regfree (LSUP_uri_ptn);
  97. free (LSUP_uri_ptn);
  98. // Free default NS map and context.
  99. LSUP_buffer_free (LSUP_default_ctx_buf);
  100. LSUP_term_free (LSUP_default_ctx);
  101. LSUP_nsmap_free (LSUP_default_nsm);
  102. // Free ID cache, including default literal datatype.
  103. hashmap_free (LSUP_term_cache);
  104. LSUP_term_cache = NULL; // This causes LSUP_IS_INIT to return false.
  105. }
  106. /*
  107. * Extern inline prototypes.
  108. */
  109. bool LSUP_is_init (void);