#include "environment.h" #include "data/bootstrap.h" /** @brief Default context. * * The actual default context IRI used throughout the application may use a * value from the `LSUP_DEFAULT_CTX_URI` environment variable. */ #define DEFAULT_CTX_LABEL LSUP_NS "default" /* * External variables. */ LSUP_TermSet *LSUP_term_cache = NULL; LSUP_NSMap *LSUP_default_nsm = NULL; LSUP_Term *LSUP_default_ctx = NULL; LSUP_Buffer *LSUP_default_ctx_buf = NULL; /* * API */ LSUP_rc LSUP_init (void) { if (LSUP_IS_INIT) { LOG_RC (LSUP_NOACTION); return LSUP_NOACTION; } #ifdef DEBUG // In debug mode, always use max logging. int loglevel = LOG_TRACE; #else char *_loglevel = getenv ("LSUP_LOGLEVEL"); int loglevel = (_loglevel == NULL) ? LOG_INFO : atoi (_loglevel); #endif log_set_level (loglevel); // URI validation pattern. MALLOC_GUARD (LSUP_uri_ptn, LSUP_MEM_ERR); #if 0 // Re-activate in case a change in the URI regex results in an error. int regex_rc = regcomp (LSUP_uri_ptn, LSUP_URI_REGEX_STR, REG_EXTENDED); if (regex_rc != 0) { char err_msg[128]; size_t err_msg_sz = regerror (regex_rc, LSUP_uri_ptn, err_msg, 128); log_error ( "Error compiling regular expression pattern: %s.", err_msg); return LSUP_ERROR; } #else if (regcomp (LSUP_uri_ptn, LSUP_URI_REGEX_STR, REG_EXTENDED) != 0) { log_error ("Error compiling regular expression pattern."); return LSUP_ERROR; } #endif // Default namespace map. LSUP_default_nsm = LSUP_nsmap_new(); if (UNLIKELY (!LSUP_default_nsm)) return LSUP_ERROR; for (int i = 0; init_nsmap[i][0] != NULL; i++) LSUP_nsmap_add (LSUP_default_nsm, init_nsmap[i][0], init_nsmap[i][1]); // Default context URI. char *default_ctx_str = getenv ("LSUP_DEFAULT_CTX"); if (!default_ctx_str ) default_ctx_str = DEFAULT_CTX_LABEL; LSUP_default_ctx = LSUP_iriref_new (default_ctx_str, NULL); if (UNLIKELY (!LSUP_default_ctx)) return LSUP_ERROR; LSUP_default_ctx_buf = LSUP_term_serialize (LSUP_default_ctx); if (UNLIKELY (!LSUP_default_ctx_buf)) return LSUP_ERROR; // Initialize term cache. LSUP_term_cache = LSUP_term_set_new(); if (UNLIKELY (!LSUP_term_cache)) return LSUP_MEM_ERR; // Create and cache default literal datatype key. // This will be done only once in the program, so no need to check for // duplicates. LSUP_default_datatype = LSUP_iriref_new (DEFAULT_DTYPE, NULL); LSUP_rc rc = LSUP_term_set_add ( LSUP_term_cache, LSUP_default_datatype, NULL); PRCCK (rc); // Set automatic teardown TODO Is this a good idea? atexit (LSUP_done); return LSUP_OK; } void LSUP_done (void) { if (!LSUP_IS_INIT) return; regfree (LSUP_uri_ptn); free (LSUP_uri_ptn); // Free default NS map and context. LSUP_buffer_free (LSUP_default_ctx_buf); LSUP_term_free (LSUP_default_ctx); LSUP_nsmap_free (LSUP_default_nsm); // Free ID cache, including default literal datatype. hashmap_free (LSUP_term_cache); LSUP_term_cache = NULL; // This causes LSUP_IS_INIT to return false. } /* * Extern inline prototypes. */ bool LSUP_is_init (void);