environment.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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_TermSet *LSUP_term_cache = NULL;
  13. LSUP_NSMap *LSUP_default_nsm = NULL;
  14. LSUP_Term *LSUP_default_ctx = NULL;
  15. LSUP_Buffer *LSUP_default_ctx_buf = NULL;
  16. /*
  17. * API
  18. */
  19. LSUP_rc
  20. LSUP_init (void)
  21. {
  22. if (LSUP_IS_INIT) {
  23. LOG_RC (LSUP_NOACTION);
  24. return LSUP_NOACTION;
  25. }
  26. #ifdef DEBUG
  27. // In debug mode, always use max logging.
  28. int loglevel = LOG_TRACE;
  29. #else
  30. char *_loglevel = getenv ("LSUP_LOGLEVEL");
  31. int loglevel = (_loglevel == NULL) ? LOG_INFO : atoi (_loglevel);
  32. #endif
  33. log_set_level (loglevel);
  34. // URI validation pattern.
  35. MALLOC_GUARD (LSUP_uri_ptn, LSUP_MEM_ERR);
  36. #if 0 // Re-activate in case a change in the URI regex results in an error.
  37. int regex_rc = regcomp (LSUP_uri_ptn, LSUP_URI_REGEX_STR, REG_EXTENDED);
  38. if (regex_rc != 0) {
  39. char err_msg[128];
  40. size_t err_msg_sz = regerror (regex_rc, LSUP_uri_ptn, err_msg, 128);
  41. log_error (
  42. "Error compiling regular expression pattern: %s.",
  43. err_msg);
  44. return LSUP_ERROR;
  45. }
  46. #else
  47. if (regcomp (LSUP_uri_ptn, LSUP_URI_REGEX_STR, REG_EXTENDED) != 0) {
  48. log_error ("Error compiling regular expression pattern.");
  49. return LSUP_ERROR;
  50. }
  51. #endif
  52. // Default namespace map.
  53. LSUP_default_nsm = LSUP_nsmap_new();
  54. if (UNLIKELY (!LSUP_default_nsm)) return LSUP_ERROR;
  55. for (int i = 0; init_nsmap[i][0] != NULL; i++)
  56. LSUP_nsmap_add (LSUP_default_nsm, init_nsmap[i][0], init_nsmap[i][1]);
  57. // Default context URI.
  58. char *default_ctx_str = getenv ("LSUP_DEFAULT_CTX");
  59. if (!default_ctx_str ) default_ctx_str = DEFAULT_CTX_LABEL;
  60. LSUP_default_ctx = LSUP_iriref_new (default_ctx_str, NULL);
  61. if (UNLIKELY (!LSUP_default_ctx)) return LSUP_ERROR;
  62. LSUP_default_ctx_buf = LSUP_term_serialize (LSUP_default_ctx);
  63. if (UNLIKELY (!LSUP_default_ctx_buf)) return LSUP_ERROR;
  64. // Initialize term cache.
  65. LSUP_term_cache = LSUP_term_set_new();
  66. if (UNLIKELY (!LSUP_term_cache)) return LSUP_MEM_ERR;
  67. // Create and cache default literal datatype key.
  68. // This will be done only once in the program, so no need to check for
  69. // duplicates.
  70. LSUP_default_datatype = LSUP_iriref_new (DEFAULT_DTYPE, NULL);
  71. LSUP_rc rc = LSUP_term_set_add (
  72. LSUP_term_cache, LSUP_default_datatype, NULL);
  73. PRCCK (rc);
  74. // Set automatic teardown TODO Is this a good idea?
  75. atexit (LSUP_done);
  76. return LSUP_OK;
  77. }
  78. void
  79. LSUP_done (void)
  80. {
  81. if (!LSUP_IS_INIT) return;
  82. regfree (LSUP_uri_ptn);
  83. free (LSUP_uri_ptn);
  84. // Free default NS map and context.
  85. LSUP_buffer_free (LSUP_default_ctx_buf);
  86. LSUP_term_free (LSUP_default_ctx);
  87. LSUP_nsmap_free (LSUP_default_nsm);
  88. // Free ID cache, including default literal datatype.
  89. hashmap_free (LSUP_term_cache);
  90. LSUP_term_cache = NULL; // This causes LSUP_IS_INIT to return false.
  91. }
  92. /*
  93. * Extern inline prototypes.
  94. */
  95. bool LSUP_is_init (void);