1 /* Copyright (c) 2013, Vsevolod Stakhov
2 * Copyright (c) 2015 Allan Jude <allanjude@freebsd.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16 * DISCLAIMED. IN NO EVENT SHALL AUTHOR BE LIABLE FOR ANY
17 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
20 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
22 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 #include "ucl_internal.h"
27 #include "ucl_chartable.h"
30 #include <stdio.h> /* for snprintf */
37 #include <libgen.h> /* For dirname */
40 typedef kvec_t(ucl_object_t *) ucl_array_t;
42 #define UCL_ARRAY_GET(ar, obj) ucl_array_t *ar = \
43 (ucl_array_t *)((obj) != NULL ? (obj)->value.av : NULL)
46 #include <openssl/err.h>
47 #include <openssl/sha.h>
48 #include <openssl/rsa.h>
49 #include <openssl/ssl.h>
50 #include <openssl/evp.h>
54 /* Seems to be broken */
55 #define CURL_DISABLE_TYPECHECK 1
56 #include <curl/curl.h>
71 #ifndef PROT_READWRITE
72 #define PROT_READWRITE 3
81 #define MAP_FAILED ((void *) -1)
89 static void *ucl_mmap(char *addr, size_t length, int prot, int access, int fd, off_t offset)
92 HANDLE handle = INVALID_HANDLE_VALUE;
98 handle = CreateFileMapping((HANDLE) _get_osfhandle(fd), 0, PAGE_READONLY, 0, length, 0);
100 map = (void *) MapViewOfFile(handle, FILE_MAP_READ, 0, 0, length);
106 handle = CreateFileMapping((HANDLE) _get_osfhandle(fd), 0, PAGE_READWRITE, 0, length, 0);
108 map = (void *) MapViewOfFile(handle, FILE_MAP_WRITE, 0, 0, length);
114 handle = CreateFileMapping((HANDLE) _get_osfhandle(fd), 0, PAGE_READWRITE, 0, length, 0);
116 map = (void *) MapViewOfFile(handle, FILE_MAP_ALL_ACCESS, 0, 0, length);
121 if (map == (void *) NULL) {
122 return (void *) MAP_FAILED;
124 return (void *) ((char *) map + offset);
127 static int ucl_munmap(void *map,size_t length)
129 if (!UnmapViewOfFile(map)) {
135 static char* ucl_realpath(const char *path, char *resolved_path) {
137 char tmp[MAX_PATH + 1];
138 strncpy(tmp, path, sizeof(tmp)-1);
141 if (*p == '/') *p = '\\';
144 return _fullpath(resolved_path, tmp, MAX_PATH);
147 #define ucl_mmap mmap
148 #define ucl_munmap munmap
149 #define ucl_realpath realpath
152 typedef void (*ucl_object_dtor) (ucl_object_t *obj);
153 static void ucl_object_free_internal (ucl_object_t *obj, bool allow_rec,
154 ucl_object_dtor dtor);
155 static void ucl_object_dtor_unref (ucl_object_t *obj);
158 ucl_object_dtor_free (ucl_object_t *obj)
160 if (obj->trash_stack[UCL_TRASH_KEY] != NULL) {
161 UCL_FREE (obj->hh.keylen, obj->trash_stack[UCL_TRASH_KEY]);
163 if (obj->trash_stack[UCL_TRASH_VALUE] != NULL) {
164 UCL_FREE (obj->len, obj->trash_stack[UCL_TRASH_VALUE]);
166 /* Do not free ephemeral objects */
167 if ((obj->flags & UCL_OBJECT_EPHEMERAL) == 0) {
168 if (obj->type != UCL_USERDATA) {
169 UCL_FREE (sizeof (ucl_object_t), obj);
172 struct ucl_object_userdata *ud = (struct ucl_object_userdata *)obj;
174 ud->dtor (obj->value.ud);
176 UCL_FREE (sizeof (*ud), obj);
182 * This is a helper function that performs exactly the same as
183 * `ucl_object_unref` but it doesn't iterate over elements allowing
184 * to use it for individual elements of arrays and multiple values
187 ucl_object_dtor_unref_single (ucl_object_t *obj)
190 #ifdef HAVE_ATOMIC_BUILTINS
191 unsigned int rc = __sync_sub_and_fetch (&obj->ref, 1);
194 if (--obj->ref == 0) {
196 ucl_object_free_internal (obj, false, ucl_object_dtor_unref);
202 ucl_object_dtor_unref (ucl_object_t *obj)
205 ucl_object_dtor_free (obj);
208 /* This may cause dtor unref being called one more time */
209 ucl_object_dtor_unref_single (obj);
214 ucl_object_free_internal (ucl_object_t *obj, bool allow_rec, ucl_object_dtor dtor)
216 ucl_object_t *tmp, *sub;
218 while (obj != NULL) {
219 if (obj->type == UCL_ARRAY) {
220 UCL_ARRAY_GET (vec, obj);
224 for (i = 0; i < vec->n; i ++) {
225 sub = kv_A (*vec, i);
236 UCL_FREE (sizeof (*vec), vec);
238 obj->value.av = NULL;
240 else if (obj->type == UCL_OBJECT) {
241 if (obj->value.ov != NULL) {
242 ucl_hash_destroy (obj->value.ov, (ucl_hash_free_func)dtor);
244 obj->value.ov = NULL;
257 ucl_object_free (ucl_object_t *obj)
259 ucl_object_free_internal (obj, true, ucl_object_dtor_free);
263 ucl_unescape_json_string (char *str, size_t len)
265 char *t = str, *h = str;
271 /* t is target (tortoise), h is source (hare) */
279 * If \ is last, then do not try to go further
312 h ++; /* u character */
316 for (i = 0; i < 4; i++) {
318 if (isdigit (h[i])) {
321 else if (h[i] >= 'a' && h[i] <= 'f') {
322 uval += h[i] - 'a' + 10;
324 else if (h[i] >= 'A' && h[i] <= 'F') {
325 uval += h[i] - 'A' + 10;
337 else if(uval < 0x800) {
338 t[0] = 0xC0 + ((uval & 0x7C0) >> 6);
339 t[1] = 0x80 + ((uval & 0x03F));
342 else if(uval < 0x10000) {
343 t[0] = 0xE0 + ((uval & 0xF000) >> 12);
344 t[1] = 0x80 + ((uval & 0x0FC0) >> 6);
345 t[2] = 0x80 + ((uval & 0x003F));
349 /* It's not actually supported now */
350 else if(uval <= 0x10FFFF) {
351 t[0] = 0xF0 + ((uval & 0x1C0000) >> 18);
352 t[1] = 0x80 + ((uval & 0x03F000) >> 12);
353 t[2] = 0x80 + ((uval & 0x000FC0) >> 6);
354 t[3] = 0x80 + ((uval & 0x00003F));
362 /* Consume 4 characters of source */
367 len --; /* for '\' character */
396 ucl_copy_key_trash (const ucl_object_t *obj)
398 ucl_object_t *deconst;
403 if (obj->trash_stack[UCL_TRASH_KEY] == NULL && obj->key != NULL) {
404 deconst = __DECONST (ucl_object_t *, obj);
405 deconst->trash_stack[UCL_TRASH_KEY] = malloc (obj->keylen + 1);
406 if (deconst->trash_stack[UCL_TRASH_KEY] != NULL) {
407 memcpy (deconst->trash_stack[UCL_TRASH_KEY], obj->key, obj->keylen);
408 deconst->trash_stack[UCL_TRASH_KEY][obj->keylen] = '\0';
410 deconst->key = obj->trash_stack[UCL_TRASH_KEY];
411 deconst->flags |= UCL_OBJECT_ALLOCATED_KEY;
414 return obj->trash_stack[UCL_TRASH_KEY];
418 ucl_copy_value_trash (const ucl_object_t *obj)
420 ucl_object_t *deconst;
425 if (obj->trash_stack[UCL_TRASH_VALUE] == NULL) {
426 deconst = __DECONST (ucl_object_t *, obj);
427 if (obj->type == UCL_STRING) {
429 /* Special case for strings */
430 if (obj->flags & UCL_OBJECT_BINARY) {
431 deconst->trash_stack[UCL_TRASH_VALUE] = malloc (obj->len);
432 if (deconst->trash_stack[UCL_TRASH_VALUE] != NULL) {
433 memcpy (deconst->trash_stack[UCL_TRASH_VALUE],
436 deconst->value.sv = obj->trash_stack[UCL_TRASH_VALUE];
440 deconst->trash_stack[UCL_TRASH_VALUE] = malloc (obj->len + 1);
441 if (deconst->trash_stack[UCL_TRASH_VALUE] != NULL) {
442 memcpy (deconst->trash_stack[UCL_TRASH_VALUE],
445 deconst->trash_stack[UCL_TRASH_VALUE][obj->len] = '\0';
446 deconst->value.sv = obj->trash_stack[UCL_TRASH_VALUE];
451 /* Just emit value in json notation */
452 deconst->trash_stack[UCL_TRASH_VALUE] = ucl_object_emit_single_json (obj);
453 deconst->len = strlen (obj->trash_stack[UCL_TRASH_VALUE]);
455 deconst->flags |= UCL_OBJECT_ALLOCATED_VALUE;
458 return obj->trash_stack[UCL_TRASH_VALUE];
462 ucl_parser_get_object (struct ucl_parser *parser)
464 if (parser->state != UCL_STATE_ERROR && parser->top_obj != NULL) {
465 return ucl_object_ref (parser->top_obj);
472 ucl_parser_free (struct ucl_parser *parser)
474 struct ucl_stack *stack, *stmp;
475 struct ucl_macro *macro, *mtmp;
476 struct ucl_chunk *chunk, *ctmp;
477 struct ucl_pubkey *key, *ktmp;
478 struct ucl_variable *var, *vtmp;
479 ucl_object_t *tr, *trtmp;
481 if (parser == NULL) {
485 if (parser->top_obj != NULL) {
486 ucl_object_unref (parser->top_obj);
489 if (parser->includepaths != NULL) {
490 ucl_object_unref (parser->includepaths);
493 LL_FOREACH_SAFE (parser->stack, stack, stmp) {
496 HASH_ITER (hh, parser->macroes, macro, mtmp) {
498 HASH_DEL (parser->macroes, macro);
499 UCL_FREE (sizeof (struct ucl_macro), macro);
501 LL_FOREACH_SAFE (parser->chunks, chunk, ctmp) {
502 UCL_FREE (sizeof (struct ucl_chunk), chunk);
504 LL_FOREACH_SAFE (parser->keys, key, ktmp) {
505 UCL_FREE (sizeof (struct ucl_pubkey), key);
507 LL_FOREACH_SAFE (parser->variables, var, vtmp) {
510 UCL_FREE (sizeof (struct ucl_variable), var);
512 LL_FOREACH_SAFE (parser->trash_objs, tr, trtmp) {
513 ucl_object_free_internal (tr, false, ucl_object_dtor_free);
516 if (parser->err != NULL) {
517 utstring_free (parser->err);
520 if (parser->cur_file) {
521 free (parser->cur_file);
524 if (parser->comments) {
525 ucl_object_unref (parser->comments);
528 UCL_FREE (sizeof (struct ucl_parser), parser);
532 ucl_parser_get_error(struct ucl_parser *parser)
534 if (parser == NULL) {
538 if (parser->err == NULL) {
542 return utstring_body (parser->err);
546 ucl_parser_get_error_code(struct ucl_parser *parser)
548 if (parser == NULL) {
552 return parser->err_code;
556 ucl_parser_get_column(struct ucl_parser *parser)
558 if (parser == NULL || parser->chunks == NULL) {
562 return parser->chunks->column;
566 ucl_parser_get_linenum(struct ucl_parser *parser)
568 if (parser == NULL || parser->chunks == NULL) {
572 return parser->chunks->line;
576 ucl_parser_clear_error(struct ucl_parser *parser)
578 if (parser != NULL && parser->err != NULL) {
579 utstring_free(parser->err);
581 parser->err_code = 0;
586 ucl_pubkey_add (struct ucl_parser *parser, const unsigned char *key, size_t len)
589 ucl_create_err (&parser->err, "cannot check signatures without openssl");
592 # if (OPENSSL_VERSION_NUMBER < 0x10000000L)
593 ucl_create_err (&parser->err, "cannot check signatures, openssl version is unsupported");
596 struct ucl_pubkey *nkey;
599 mem = BIO_new_mem_buf ((void *)key, len);
600 nkey = UCL_ALLOC (sizeof (struct ucl_pubkey));
602 ucl_create_err (&parser->err, "cannot allocate memory for key");
605 nkey->key = PEM_read_bio_PUBKEY (mem, &nkey->key, NULL, NULL);
607 if (nkey->key == NULL) {
608 UCL_FREE (sizeof (struct ucl_pubkey), nkey);
609 ucl_create_err (&parser->err, "%s",
610 ERR_error_string (ERR_get_error (), NULL));
613 LL_PREPEND (parser->keys, nkey);
620 struct ucl_curl_cbdata {
626 ucl_curl_write_callback (void* contents, size_t size, size_t nmemb, void* ud)
628 struct ucl_curl_cbdata *cbdata = ud;
629 size_t realsize = size * nmemb;
631 cbdata->buf = realloc (cbdata->buf, cbdata->buflen + realsize + 1);
632 if (cbdata->buf == NULL) {
636 memcpy (&(cbdata->buf[cbdata->buflen]), contents, realsize);
637 cbdata->buflen += realsize;
638 cbdata->buf[cbdata->buflen] = 0;
645 * Fetch a url and save results to the memory buffer
646 * @param url url to fetch
647 * @param len length of url
648 * @param buf target buffer
649 * @param buflen target length
653 ucl_fetch_url (const unsigned char *url, unsigned char **buf, size_t *buflen,
654 UT_string **err, bool must_exist)
658 struct url *fetch_url;
662 fetch_url = fetchParseURL (url);
663 if (fetch_url == NULL) {
664 ucl_create_err (err, "invalid URL %s: %s",
665 url, strerror (errno));
668 if ((in = fetchXGet (fetch_url, &us, "")) == NULL) {
670 ucl_create_err (err, "cannot fetch URL %s: %s",
671 url, strerror (errno));
673 fetchFreeURL (fetch_url);
678 *buf = malloc (*buflen);
680 ucl_create_err (err, "cannot allocate buffer for URL %s: %s",
681 url, strerror (errno));
683 fetchFreeURL (fetch_url);
687 if (fread (*buf, *buflen, 1, in) != 1) {
688 ucl_create_err (err, "cannot read URL %s: %s",
689 url, strerror (errno));
691 fetchFreeURL (fetch_url);
695 fetchFreeURL (fetch_url);
697 #elif defined(CURL_FOUND)
700 struct ucl_curl_cbdata cbdata;
702 curl = curl_easy_init ();
704 ucl_create_err (err, "CURL interface is broken");
707 if ((r = curl_easy_setopt (curl, CURLOPT_URL, url)) != CURLE_OK) {
708 ucl_create_err (err, "invalid URL %s: %s",
709 url, curl_easy_strerror (r));
710 curl_easy_cleanup (curl);
713 curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, ucl_curl_write_callback);
716 curl_easy_setopt (curl, CURLOPT_WRITEDATA, &cbdata);
718 if ((r = curl_easy_perform (curl)) != CURLE_OK) {
720 ucl_create_err (err, "error fetching URL %s: %s",
721 url, curl_easy_strerror (r));
723 curl_easy_cleanup (curl);
730 *buflen = cbdata.buflen;
734 ucl_create_err (err, "URL support is disabled");
740 * Fetch a file and save results to the memory buffer
741 * @param filename filename to fetch
742 * @param len length of filename
743 * @param buf target buffer
744 * @param buflen target length
748 ucl_fetch_file (const unsigned char *filename, unsigned char **buf, size_t *buflen,
749 UT_string **err, bool must_exist)
754 if (stat (filename, &st) == -1 || !S_ISREG (st.st_mode)) {
756 ucl_create_err (err, "cannot stat file %s: %s",
757 filename, strerror (errno));
761 if (st.st_size == 0) {
762 /* Do not map empty files */
767 if ((fd = open (filename, O_RDONLY)) == -1) {
768 ucl_create_err (err, "cannot open file %s: %s",
769 filename, strerror (errno));
772 if ((*buf = ucl_mmap (NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0)) == MAP_FAILED) {
774 ucl_create_err (err, "cannot mmap file %s: %s",
775 filename, strerror (errno));
780 *buflen = st.st_size;
788 #if (defined(HAVE_OPENSSL) && OPENSSL_VERSION_NUMBER >= 0x10000000L)
790 ucl_sig_check (const unsigned char *data, size_t datalen,
791 const unsigned char *sig, size_t siglen, struct ucl_parser *parser)
793 struct ucl_pubkey *key;
794 char dig[EVP_MAX_MD_SIZE];
796 EVP_PKEY_CTX *key_ctx;
797 EVP_MD_CTX *sign_ctx = NULL;
799 sign_ctx = EVP_MD_CTX_create ();
801 LL_FOREACH (parser->keys, key) {
802 key_ctx = EVP_PKEY_CTX_new (key->key, NULL);
803 if (key_ctx != NULL) {
804 if (EVP_PKEY_verify_init (key_ctx) <= 0) {
805 EVP_PKEY_CTX_free (key_ctx);
808 if (EVP_PKEY_CTX_set_rsa_padding (key_ctx, RSA_PKCS1_PADDING) <= 0) {
809 EVP_PKEY_CTX_free (key_ctx);
812 if (EVP_PKEY_CTX_set_signature_md (key_ctx, EVP_sha256 ()) <= 0) {
813 EVP_PKEY_CTX_free (key_ctx);
816 EVP_DigestInit (sign_ctx, EVP_sha256 ());
817 EVP_DigestUpdate (sign_ctx, data, datalen);
818 EVP_DigestFinal (sign_ctx, dig, &diglen);
820 if (EVP_PKEY_verify (key_ctx, sig, siglen, dig, diglen) == 1) {
821 EVP_MD_CTX_destroy (sign_ctx);
822 EVP_PKEY_CTX_free (key_ctx);
826 EVP_PKEY_CTX_free (key_ctx);
830 EVP_MD_CTX_destroy (sign_ctx);
836 struct ucl_include_params {
837 bool check_signature;
844 enum ucl_duplicate_strategy strat;
845 enum ucl_parse_type parse_type;
851 * Include an url to configuration
859 ucl_include_url (const unsigned char *data, size_t len,
860 struct ucl_parser *parser,
861 struct ucl_include_params *params)
865 unsigned char *buf = NULL;
867 struct ucl_chunk *chunk;
868 char urlbuf[PATH_MAX];
871 snprintf (urlbuf, sizeof (urlbuf), "%.*s", (int)len, data);
873 if (!ucl_fetch_url (urlbuf, &buf, &buflen, &parser->err, params->must_exist)) {
874 return !params->must_exist;
877 if (params->check_signature) {
878 #if (defined(HAVE_OPENSSL) && OPENSSL_VERSION_NUMBER >= 0x10000000L)
879 unsigned char *sigbuf = NULL;
881 /* We need to check signature first */
882 snprintf (urlbuf, sizeof (urlbuf), "%.*s.sig", (int)len, data);
883 if (!ucl_fetch_url (urlbuf, &sigbuf, &siglen, &parser->err, true)) {
886 if (!ucl_sig_check (buf, buflen, sigbuf, siglen, parser)) {
887 ucl_create_err (&parser->err, "cannot verify url %s: %s",
889 ERR_error_string (ERR_get_error (), NULL));
891 ucl_munmap (sigbuf, siglen);
896 ucl_munmap (sigbuf, siglen);
901 prev_state = parser->state;
902 parser->state = UCL_STATE_INIT;
904 res = ucl_parser_add_chunk_full (parser, buf, buflen, params->priority,
905 params->strat, params->parse_type);
907 /* Remove chunk from the stack */
908 chunk = parser->chunks;
910 parser->chunks = chunk->next;
911 UCL_FREE (sizeof (struct ucl_chunk), chunk);
915 parser->state = prev_state;
922 * Include a single file to the parser
926 * @param check_signature
933 ucl_include_file_single (const unsigned char *data, size_t len,
934 struct ucl_parser *parser, struct ucl_include_params *params)
937 struct ucl_chunk *chunk;
938 unsigned char *buf = NULL;
939 char *old_curfile, *ext;
941 char filebuf[PATH_MAX], realbuf[PATH_MAX];
943 struct ucl_variable *cur_var, *tmp_var, *old_curdir = NULL,
944 *old_filename = NULL;
945 ucl_object_t *nest_obj = NULL, *old_obj = NULL, *new_obj = NULL;
946 ucl_hash_t *container = NULL;
947 struct ucl_stack *st = NULL;
949 snprintf (filebuf, sizeof (filebuf), "%.*s", (int)len, data);
950 if (ucl_realpath (filebuf, realbuf) == NULL) {
951 if (params->soft_fail) {
954 if (!params->must_exist) {
957 ucl_create_err (&parser->err, "cannot open file %s: %s",
963 if (parser->cur_file && strcmp (realbuf, parser->cur_file) == 0) {
964 /* We are likely including the file itself */
965 if (params->soft_fail) {
969 ucl_create_err (&parser->err, "trying to include the file %s from itself",
974 if (!ucl_fetch_file (realbuf, &buf, &buflen, &parser->err, params->must_exist)) {
975 if (params->soft_fail) {
979 return (!params->must_exist || false);
982 if (params->check_signature) {
983 #if (defined(HAVE_OPENSSL) && OPENSSL_VERSION_NUMBER >= 0x10000000L)
984 unsigned char *sigbuf = NULL;
986 /* We need to check signature first */
987 snprintf (filebuf, sizeof (filebuf), "%s.sig", realbuf);
988 if (!ucl_fetch_file (filebuf, &sigbuf, &siglen, &parser->err, true)) {
991 if (!ucl_sig_check (buf, buflen, sigbuf, siglen, parser)) {
992 ucl_create_err (&parser->err, "cannot verify file %s: %s",
994 ERR_error_string (ERR_get_error (), NULL));
996 ucl_munmap (sigbuf, siglen);
1001 ucl_munmap (sigbuf, siglen);
1006 old_curfile = parser->cur_file;
1007 parser->cur_file = strdup (realbuf);
1009 /* Store old file vars */
1010 DL_FOREACH_SAFE (parser->variables, cur_var, tmp_var) {
1011 if (strcmp (cur_var->var, "CURDIR") == 0) {
1012 old_curdir = cur_var;
1013 DL_DELETE (parser->variables, cur_var);
1015 else if (strcmp (cur_var->var, "FILENAME") == 0) {
1016 old_filename = cur_var;
1017 DL_DELETE (parser->variables, cur_var);
1021 ucl_parser_set_filevars (parser, realbuf, false);
1023 prev_state = parser->state;
1024 parser->state = UCL_STATE_INIT;
1026 if (params->use_prefix && params->prefix == NULL) {
1027 /* Auto generate a key name based on the included filename */
1028 params->prefix = basename (realbuf);
1029 ext = strrchr (params->prefix, '.');
1030 if (ext != NULL && (strcmp (ext, ".conf") == 0 || strcmp (ext, ".ucl") == 0)) {
1031 /* Strip off .conf or .ucl */
1035 if (params->prefix != NULL) {
1036 /* This is a prefixed include */
1037 container = parser->stack->obj->value.ov;
1039 old_obj = __DECONST (ucl_object_t *, ucl_hash_search (container,
1040 params->prefix, strlen (params->prefix)));
1042 if (strcasecmp (params->target, "array") == 0 && old_obj == NULL) {
1043 /* Create an array with key: prefix */
1044 old_obj = ucl_object_new_full (UCL_ARRAY, params->priority);
1045 old_obj->key = params->prefix;
1046 old_obj->keylen = strlen (params->prefix);
1047 ucl_copy_key_trash(old_obj);
1048 old_obj->prev = old_obj;
1049 old_obj->next = NULL;
1051 container = ucl_hash_insert_object (container, old_obj,
1052 parser->flags & UCL_PARSER_KEY_LOWERCASE);
1053 parser->stack->obj->len ++;
1055 nest_obj = ucl_object_new_full (UCL_OBJECT, params->priority);
1056 nest_obj->prev = nest_obj;
1057 nest_obj->next = NULL;
1059 ucl_array_append (old_obj, nest_obj);
1061 else if (old_obj == NULL) {
1062 /* Create an object with key: prefix */
1063 nest_obj = ucl_object_new_full (UCL_OBJECT, params->priority);
1065 if (nest_obj == NULL) {
1066 ucl_create_err (&parser->err, "cannot allocate memory for an object");
1068 ucl_munmap (buf, buflen);
1074 nest_obj->key = params->prefix;
1075 nest_obj->keylen = strlen (params->prefix);
1076 ucl_copy_key_trash(nest_obj);
1077 nest_obj->prev = nest_obj;
1078 nest_obj->next = NULL;
1080 container = ucl_hash_insert_object (container, nest_obj,
1081 parser->flags & UCL_PARSER_KEY_LOWERCASE);
1082 parser->stack->obj->len ++;
1084 else if (strcasecmp (params->target, "array") == 0 ||
1085 ucl_object_type(old_obj) == UCL_ARRAY) {
1086 if (ucl_object_type(old_obj) == UCL_ARRAY) {
1087 /* Append to the existing array */
1088 nest_obj = ucl_object_new_full (UCL_OBJECT, params->priority);
1089 if (nest_obj == NULL) {
1090 ucl_create_err (&parser->err, "cannot allocate memory for an object");
1092 ucl_munmap (buf, buflen);
1097 nest_obj->prev = nest_obj;
1098 nest_obj->next = NULL;
1100 ucl_array_append (old_obj, nest_obj);
1103 /* Convert the object to an array */
1104 new_obj = ucl_object_typed_new (UCL_ARRAY);
1105 if (new_obj == NULL) {
1106 ucl_create_err (&parser->err, "cannot allocate memory for an object");
1108 ucl_munmap (buf, buflen);
1113 new_obj->key = old_obj->key;
1114 new_obj->keylen = old_obj->keylen;
1115 new_obj->flags |= UCL_OBJECT_MULTIVALUE;
1116 new_obj->prev = new_obj;
1117 new_obj->next = NULL;
1119 nest_obj = ucl_object_new_full (UCL_OBJECT, params->priority);
1120 if (nest_obj == NULL) {
1121 ucl_create_err (&parser->err, "cannot allocate memory for an object");
1123 ucl_munmap (buf, buflen);
1128 nest_obj->prev = nest_obj;
1129 nest_obj->next = NULL;
1131 ucl_array_append (new_obj, old_obj);
1132 ucl_array_append (new_obj, nest_obj);
1133 ucl_hash_replace (container, old_obj, new_obj);
1137 if (ucl_object_type (old_obj) == UCL_OBJECT) {
1138 /* Append to existing Object*/
1142 /* The key is not an object */
1143 ucl_create_err (&parser->err,
1144 "Conflicting type for key: %s",
1147 ucl_munmap (buf, buflen);
1154 /* Put all of the content of the include inside that object */
1155 parser->stack->obj->value.ov = container;
1157 st = UCL_ALLOC (sizeof (struct ucl_stack));
1159 ucl_create_err (&parser->err, "cannot allocate memory for an object");
1160 ucl_object_unref (nest_obj);
1163 ucl_munmap (buf, buflen);
1169 st->level = parser->stack->level;
1170 LL_PREPEND (parser->stack, st);
1171 parser->cur_obj = nest_obj;
1174 res = ucl_parser_add_chunk_full (parser, buf, buflen, params->priority,
1175 params->strat, params->parse_type);
1178 if (!params->must_exist) {
1180 utstring_free (parser->err);
1186 /* Stop nesting the include, take 1 level off the stack */
1187 if (params->prefix != NULL && nest_obj != NULL) {
1188 parser->stack = st->next;
1189 UCL_FREE (sizeof (struct ucl_stack), st);
1192 /* Remove chunk from the stack */
1193 chunk = parser->chunks;
1194 if (chunk != NULL) {
1195 parser->chunks = chunk->next;
1196 UCL_FREE (sizeof (struct ucl_chunk), chunk);
1197 parser->recursion --;
1200 /* Restore old file vars */
1201 if (parser->cur_file) {
1202 free (parser->cur_file);
1205 parser->cur_file = old_curfile;
1206 DL_FOREACH_SAFE (parser->variables, cur_var, tmp_var) {
1207 if (strcmp (cur_var->var, "CURDIR") == 0 && old_curdir) {
1208 DL_DELETE (parser->variables, cur_var);
1209 free (cur_var->var);
1210 free (cur_var->value);
1211 UCL_FREE (sizeof (struct ucl_variable), cur_var);
1213 else if (strcmp (cur_var->var, "FILENAME") == 0 && old_filename) {
1214 DL_DELETE (parser->variables, cur_var);
1215 free (cur_var->var);
1216 free (cur_var->value);
1217 UCL_FREE (sizeof (struct ucl_variable), cur_var);
1221 DL_APPEND (parser->variables, old_filename);
1224 DL_APPEND (parser->variables, old_curdir);
1227 parser->state = prev_state;
1230 ucl_munmap (buf, buflen);
1237 * Include a file to configuration
1245 ucl_include_file (const unsigned char *data, size_t len,
1246 struct ucl_parser *parser, struct ucl_include_params *params)
1248 const unsigned char *p = data, *end = data + len;
1249 bool need_glob = false;
1251 char glob_pattern[PATH_MAX];
1255 if (!params->allow_glob) {
1256 return ucl_include_file_single (data, len, parser, params);
1259 /* Check for special symbols in a filename */
1261 if (*p == '*' || *p == '?') {
1269 memset (&globbuf, 0, sizeof (globbuf));
1270 ucl_strlcpy (glob_pattern, (const char *)data,
1271 (len + 1 < sizeof (glob_pattern) ? len + 1 : sizeof (glob_pattern)));
1272 if (glob (glob_pattern, 0, NULL, &globbuf) != 0) {
1273 return (!params->must_exist || false);
1275 for (i = 0; i < globbuf.gl_pathc; i ++) {
1276 if (!ucl_include_file_single ((unsigned char *)globbuf.gl_pathv[i],
1277 strlen (globbuf.gl_pathv[i]), parser, params)) {
1278 if (params->soft_fail) {
1281 globfree (&globbuf);
1286 globfree (&globbuf);
1288 if (cnt == 0 && params->must_exist) {
1289 ucl_create_err (&parser->err, "cannot match any files for pattern %s",
1295 return ucl_include_file_single (data, len, parser, params);
1299 /* Win32 compilers do not support globbing. Therefore, for Win32,
1300 treat allow_glob/need_glob as a NOOP and just return */
1301 return ucl_include_file_single (data, len, parser, params);
1308 * Common function to handle .*include* macros
1313 * @param default_try
1314 * @param default_sign
1318 ucl_include_common (const unsigned char *data, size_t len,
1319 const ucl_object_t *args, struct ucl_parser *parser,
1323 bool allow_url = false, search = false;
1324 const char *duplicate;
1325 const ucl_object_t *param;
1326 ucl_object_iter_t it = NULL, ip = NULL;
1327 char ipath[PATH_MAX];
1328 struct ucl_include_params params;
1330 /* Default values */
1331 params.soft_fail = default_try;
1332 params.allow_glob = false;
1333 params.check_signature = default_sign;
1334 params.use_prefix = false;
1335 params.target = "object";
1336 params.prefix = NULL;
1337 params.priority = 0;
1338 params.parse_type = UCL_PARSE_UCL;
1339 params.strat = UCL_DUPLICATE_APPEND;
1340 params.must_exist = !default_try;
1342 /* Process arguments */
1343 if (args != NULL && args->type == UCL_OBJECT) {
1344 while ((param = ucl_object_iterate (args, &it, true)) != NULL) {
1345 if (param->type == UCL_BOOLEAN) {
1346 if (strncmp (param->key, "try", param->keylen) == 0) {
1347 params.must_exist = !ucl_object_toboolean (param);
1349 else if (strncmp (param->key, "sign", param->keylen) == 0) {
1350 params.check_signature = ucl_object_toboolean (param);
1352 else if (strncmp (param->key, "glob", param->keylen) == 0) {
1353 params.allow_glob = ucl_object_toboolean (param);
1355 else if (strncmp (param->key, "url", param->keylen) == 0) {
1356 allow_url = ucl_object_toboolean (param);
1358 else if (strncmp (param->key, "prefix", param->keylen) == 0) {
1359 params.use_prefix = ucl_object_toboolean (param);
1362 else if (param->type == UCL_STRING) {
1363 if (strncmp (param->key, "key", param->keylen) == 0) {
1364 params.prefix = ucl_object_tostring (param);
1366 else if (strncmp (param->key, "target", param->keylen) == 0) {
1367 params.target = ucl_object_tostring (param);
1369 else if (strncmp (param->key, "duplicate", param->keylen) == 0) {
1370 duplicate = ucl_object_tostring (param);
1372 if (strcmp (duplicate, "append") == 0) {
1373 params.strat = UCL_DUPLICATE_APPEND;
1375 else if (strcmp (duplicate, "merge") == 0) {
1376 params.strat = UCL_DUPLICATE_MERGE;
1378 else if (strcmp (duplicate, "rewrite") == 0) {
1379 params.strat = UCL_DUPLICATE_REWRITE;
1381 else if (strcmp (duplicate, "error") == 0) {
1382 params.strat = UCL_DUPLICATE_ERROR;
1386 else if (param->type == UCL_ARRAY) {
1387 if (strncmp (param->key, "path", param->keylen) == 0) {
1388 ucl_set_include_path (parser, __DECONST(ucl_object_t *, param));
1391 else if (param->type == UCL_INT) {
1392 if (strncmp (param->key, "priority", param->keylen) == 0) {
1393 params.priority = ucl_object_toint (param);
1399 if (parser->includepaths == NULL) {
1400 if (allow_url && ucl_strnstr (data, "://", len) != NULL) {
1401 /* Globbing is not used for URL's */
1402 return ucl_include_url (data, len, parser, ¶ms);
1404 else if (data != NULL) {
1405 /* Try to load a file */
1406 return ucl_include_file (data, len, parser, ¶ms);
1410 if (allow_url && ucl_strnstr (data, "://", len) != NULL) {
1411 /* Globbing is not used for URL's */
1412 return ucl_include_url (data, len, parser, ¶ms);
1415 ip = ucl_object_iterate_new (parser->includepaths);
1416 while ((param = ucl_object_iterate_safe (ip, true)) != NULL) {
1417 if (ucl_object_type(param) == UCL_STRING) {
1418 snprintf (ipath, sizeof (ipath), "%s/%.*s", ucl_object_tostring(param),
1420 if ((search = ucl_include_file (ipath, strlen (ipath),
1421 parser, ¶ms))) {
1422 if (!params.allow_glob) {
1428 ucl_object_iterate_free (ip);
1429 if (search == true) {
1433 ucl_create_err (&parser->err,
1434 "cannot find file: %.*s in search path",
1444 * Handle include macro
1445 * @param data include data
1446 * @param len length of data
1447 * @param args UCL object representing arguments to the macro
1448 * @param ud user data
1452 ucl_include_handler (const unsigned char *data, size_t len,
1453 const ucl_object_t *args, void* ud)
1455 struct ucl_parser *parser = ud;
1457 return ucl_include_common (data, len, args, parser, false, false);
1461 * Handle includes macro
1462 * @param data include data
1463 * @param len length of data
1464 * @param args UCL object representing arguments to the macro
1465 * @param ud user data
1469 ucl_includes_handler (const unsigned char *data, size_t len,
1470 const ucl_object_t *args, void* ud)
1472 struct ucl_parser *parser = ud;
1474 return ucl_include_common (data, len, args, parser, false, true);
1478 * Handle tryinclude macro
1479 * @param data include data
1480 * @param len length of data
1481 * @param args UCL object representing arguments to the macro
1482 * @param ud user data
1486 ucl_try_include_handler (const unsigned char *data, size_t len,
1487 const ucl_object_t *args, void* ud)
1489 struct ucl_parser *parser = ud;
1491 return ucl_include_common (data, len, args, parser, true, false);
1495 * Handle priority macro
1496 * @param data include data
1497 * @param len length of data
1498 * @param args UCL object representing arguments to the macro
1499 * @param ud user data
1503 ucl_priority_handler (const unsigned char *data, size_t len,
1504 const ucl_object_t *args, void* ud)
1506 struct ucl_parser *parser = ud;
1507 unsigned priority = 255;
1508 const ucl_object_t *param;
1510 char *value = NULL, *leftover = NULL;
1511 ucl_object_iter_t it = NULL;
1513 if (parser == NULL) {
1517 /* Process arguments */
1518 if (args != NULL && args->type == UCL_OBJECT) {
1519 while ((param = ucl_object_iterate (args, &it, true)) != NULL) {
1520 if (param->type == UCL_INT) {
1521 if (strncmp (param->key, "priority", param->keylen) == 0) {
1522 priority = ucl_object_toint (param);
1530 value = malloc(len + 1);
1531 ucl_strlcpy(value, (const char *)data, len + 1);
1532 priority = strtol(value, &leftover, 10);
1533 if (*leftover != '\0') {
1534 ucl_create_err (&parser->err, "Invalid priority value in macro: %s",
1543 if (found == true) {
1544 parser->chunks->priority = priority;
1548 ucl_create_err (&parser->err, "Unable to parse priority macro");
1554 * @param data include data
1555 * @param len length of data
1556 * @param args UCL object representing arguments to the macro
1557 * @param ud user data
1561 ucl_load_handler (const unsigned char *data, size_t len,
1562 const ucl_object_t *args, void* ud)
1564 struct ucl_parser *parser = ud;
1565 const ucl_object_t *param;
1566 ucl_object_t *obj, *old_obj;
1567 ucl_object_iter_t it = NULL;
1568 bool try_load, multiline, test;
1569 const char *target, *prefix;
1570 char *load_file, *tmp;
1575 ucl_object_t *container = NULL;
1576 enum ucl_string_flags flags;
1578 /* Default values */
1592 if (parser == NULL) {
1596 /* Process arguments */
1597 if (args != NULL && args->type == UCL_OBJECT) {
1598 while ((param = ucl_object_iterate (args, &it, true)) != NULL) {
1599 if (param->type == UCL_BOOLEAN) {
1600 if (strncmp (param->key, "try", param->keylen) == 0) {
1601 try_load = ucl_object_toboolean (param);
1603 else if (strncmp (param->key, "multiline", param->keylen) == 0) {
1604 multiline = ucl_object_toboolean (param);
1606 else if (strncmp (param->key, "escape", param->keylen) == 0) {
1607 test = ucl_object_toboolean (param);
1609 flags |= UCL_STRING_ESCAPE;
1612 else if (strncmp (param->key, "trim", param->keylen) == 0) {
1613 test = ucl_object_toboolean (param);
1615 flags |= UCL_STRING_TRIM;
1619 else if (param->type == UCL_STRING) {
1620 if (strncmp (param->key, "key", param->keylen) == 0) {
1621 prefix = ucl_object_tostring (param);
1623 else if (strncmp (param->key, "target", param->keylen) == 0) {
1624 target = ucl_object_tostring (param);
1627 else if (param->type == UCL_INT) {
1628 if (strncmp (param->key, "priority", param->keylen) == 0) {
1629 priority = ucl_object_toint (param);
1635 if (prefix == NULL || strlen (prefix) == 0) {
1636 ucl_create_err (&parser->err, "No Key specified in load macro");
1641 load_file = malloc (len + 1);
1643 ucl_create_err (&parser->err, "cannot allocate memory for suffix");
1648 snprintf (load_file, len + 1, "%.*s", (int)len, data);
1650 if (!ucl_fetch_file (load_file, &buf, &buflen, &parser->err,
1654 return (try_load || false);
1658 container = parser->stack->obj;
1659 old_obj = __DECONST (ucl_object_t *, ucl_object_lookup (container,
1662 if (old_obj != NULL) {
1663 ucl_create_err (&parser->err, "Key %s already exists", prefix);
1665 ucl_munmap (buf, buflen);
1671 if (strcasecmp (target, "string") == 0) {
1672 obj = ucl_object_fromstring_common (buf, buflen, flags);
1673 ucl_copy_value_trash (obj);
1675 obj->flags |= UCL_OBJECT_MULTILINE;
1678 else if (strcasecmp (target, "int") == 0) {
1679 tmp = malloc (buflen + 1);
1682 ucl_create_err (&parser->err, "Memory allocation failed");
1684 ucl_munmap (buf, buflen);
1690 snprintf (tmp, buflen + 1, "%.*s", (int)buflen, buf);
1691 iv = strtoll (tmp, NULL, 10);
1692 obj = ucl_object_fromint (iv);
1697 ucl_munmap (buf, buflen);
1702 obj->keylen = strlen (prefix);
1703 ucl_copy_key_trash (obj);
1706 ucl_object_set_priority (obj, priority);
1707 ucl_object_insert_key (container, obj, obj->key, obj->keylen, false);
1713 ucl_create_err (&parser->err, "Unable to parse load macro");
1718 ucl_inherit_handler (const unsigned char *data, size_t len,
1719 const ucl_object_t *args, const ucl_object_t *ctx, void* ud)
1721 const ucl_object_t *parent, *cur;
1722 ucl_object_t *target, *copy;
1723 ucl_object_iter_t it = NULL;
1724 bool replace = false;
1725 struct ucl_parser *parser = ud;
1727 parent = ucl_object_lookup_len (ctx, data, len);
1729 /* Some sanity checks */
1730 if (parent == NULL || ucl_object_type (parent) != UCL_OBJECT) {
1731 ucl_create_err (&parser->err, "Unable to find inherited object %*.s",
1736 if (parser->stack == NULL || parser->stack->obj == NULL ||
1737 ucl_object_type (parser->stack->obj) != UCL_OBJECT) {
1738 ucl_create_err (&parser->err, "Invalid inherit context");
1742 target = parser->stack->obj;
1744 if (args && (cur = ucl_object_lookup (args, "replace")) != NULL) {
1745 replace = ucl_object_toboolean (cur);
1748 while ((cur = ucl_object_iterate (parent, &it, true))) {
1749 /* We do not replace existing keys */
1750 if (!replace && ucl_object_lookup_len (target, cur->key, cur->keylen)) {
1754 copy = ucl_object_copy (cur);
1757 copy->flags |= UCL_OBJECT_INHERITED;
1760 ucl_object_insert_key (target, copy, copy->key,
1761 copy->keylen, false);
1768 ucl_parser_set_filevars (struct ucl_parser *parser, const char *filename, bool need_expand)
1770 char realbuf[PATH_MAX], *curdir;
1772 if (filename != NULL) {
1774 if (ucl_realpath (filename, realbuf) == NULL) {
1779 ucl_strlcpy (realbuf, filename, sizeof (realbuf));
1782 /* Define variables */
1783 ucl_parser_register_variable (parser, "FILENAME", realbuf);
1784 curdir = dirname (realbuf);
1785 ucl_parser_register_variable (parser, "CURDIR", curdir);
1788 /* Set everything from the current dir */
1789 curdir = getcwd (realbuf, sizeof (realbuf));
1790 ucl_parser_register_variable (parser, "FILENAME", "undef");
1791 ucl_parser_register_variable (parser, "CURDIR", curdir);
1798 ucl_parser_add_file_full (struct ucl_parser *parser, const char *filename,
1799 unsigned priority, enum ucl_duplicate_strategy strat,
1800 enum ucl_parse_type parse_type)
1805 char realbuf[PATH_MAX];
1807 if (ucl_realpath (filename, realbuf) == NULL) {
1808 ucl_create_err (&parser->err, "cannot open file %s: %s",
1814 if (!ucl_fetch_file (realbuf, &buf, &len, &parser->err, true)) {
1818 if (parser->cur_file) {
1819 free (parser->cur_file);
1821 parser->cur_file = strdup (realbuf);
1822 ucl_parser_set_filevars (parser, realbuf, false);
1823 ret = ucl_parser_add_chunk_full (parser, buf, len, priority, strat,
1827 ucl_munmap (buf, len);
1834 ucl_parser_add_file_priority (struct ucl_parser *parser, const char *filename,
1837 if (parser == NULL) {
1841 return ucl_parser_add_file_full(parser, filename, priority,
1842 UCL_DUPLICATE_APPEND, UCL_PARSE_UCL);
1846 ucl_parser_add_file (struct ucl_parser *parser, const char *filename)
1848 if (parser == NULL) {
1852 return ucl_parser_add_file_full(parser, filename,
1853 parser->default_priority, UCL_DUPLICATE_APPEND,
1858 ucl_parser_add_fd_priority (struct ucl_parser *parser, int fd,
1866 if (fstat (fd, &st) == -1) {
1867 ucl_create_err (&parser->err, "cannot stat fd %d: %s",
1868 fd, strerror (errno));
1871 if (st.st_size == 0) {
1874 if ((buf = ucl_mmap (NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0)) == MAP_FAILED) {
1875 ucl_create_err (&parser->err, "cannot mmap fd %d: %s",
1876 fd, strerror (errno));
1880 if (parser->cur_file) {
1881 free (parser->cur_file);
1883 parser->cur_file = NULL;
1885 ret = ucl_parser_add_chunk_priority (parser, buf, len, priority);
1888 ucl_munmap (buf, len);
1895 ucl_parser_add_fd (struct ucl_parser *parser, int fd)
1897 if (parser == NULL) {
1901 return ucl_parser_add_fd_priority(parser, fd, parser->default_priority);
1905 ucl_strlcpy (char *dst, const char *src, size_t siz)
1908 const char *s = src;
1911 /* Copy as many bytes as will fit */
1914 if ((*d++ = *s++) == '\0') {
1920 if (n == 0 && siz != 0) {
1924 return (s - src - 1); /* count does not include NUL */
1928 ucl_strlcpy_unsafe (char *dst, const char *src, size_t siz)
1930 memcpy (dst, src, siz - 1);
1931 dst[siz - 1] = '\0';
1937 ucl_strlcpy_tolower (char *dst, const char *src, size_t siz)
1940 const char *s = src;
1943 /* Copy as many bytes as will fit */
1946 if ((*d++ = tolower (*s++)) == '\0') {
1952 if (n == 0 && siz != 0) {
1956 return (s - src); /* count does not include NUL */
1960 * Find the first occurrence of find in s
1963 ucl_strnstr (const char *s, const char *find, int len)
1968 if ((c = *find++) != 0) {
1969 mlen = strlen (find);
1972 if ((sc = *s++) == 0 || len-- == 0)
1975 } while (strncmp (s, find, mlen) != 0);
1982 * Find the first occurrence of find in s, ignore case.
1985 ucl_strncasestr (const char *s, const char *find, int len)
1990 if ((c = *find++) != 0) {
1992 mlen = strlen (find);
1995 if ((sc = *s++) == 0 || len-- == 0)
1997 } while (tolower (sc) != c);
1998 } while (strncasecmp (s, find, mlen) != 0);
2005 ucl_object_fromstring_common (const char *str, size_t len, enum ucl_string_flags flags)
2008 const char *start, *end, *p, *pos;
2016 obj = ucl_object_new ();
2021 if (flags & UCL_STRING_TRIM) {
2022 /* Skip leading spaces */
2023 for (start = str; (size_t)(start - str) < len; start ++) {
2024 if (!ucl_test_character (*start, UCL_CHARACTER_WHITESPACE_UNSAFE)) {
2028 /* Skip trailing spaces */
2029 for (end = str + len - 1; end > start; end --) {
2030 if (!ucl_test_character (*end, UCL_CHARACTER_WHITESPACE_UNSAFE)) {
2041 obj->type = UCL_STRING;
2042 if (flags & UCL_STRING_ESCAPE) {
2043 for (p = start, escaped_len = 0; p < end; p ++, escaped_len ++) {
2044 if (ucl_test_character (*p, UCL_CHARACTER_JSON_UNSAFE)) {
2048 dst = malloc (escaped_len + 1);
2050 for (p = start, d = dst; p < end; p ++, d ++) {
2051 if (ucl_test_character (*p, UCL_CHARACTER_JSON_UNSAFE)) {
2088 obj->value.sv = dst;
2089 obj->trash_stack[UCL_TRASH_VALUE] = dst;
2090 obj->len = escaped_len;
2094 dst = malloc (end - start + 1);
2096 ucl_strlcpy_unsafe (dst, start, end - start + 1);
2097 obj->value.sv = dst;
2098 obj->trash_stack[UCL_TRASH_VALUE] = dst;
2099 obj->len = end - start;
2102 if ((flags & UCL_STRING_PARSE) && dst != NULL) {
2103 /* Parse what we have */
2104 if (flags & UCL_STRING_PARSE_BOOLEAN) {
2105 if (!ucl_maybe_parse_boolean (obj, dst, obj->len) && (flags & UCL_STRING_PARSE_NUMBER)) {
2106 ucl_maybe_parse_number (obj, dst, dst + obj->len, &pos,
2107 flags & UCL_STRING_PARSE_DOUBLE,
2108 flags & UCL_STRING_PARSE_BYTES,
2109 flags & UCL_STRING_PARSE_TIME);
2113 ucl_maybe_parse_number (obj, dst, dst + obj->len, &pos,
2114 flags & UCL_STRING_PARSE_DOUBLE,
2115 flags & UCL_STRING_PARSE_BYTES,
2116 flags & UCL_STRING_PARSE_TIME);
2125 ucl_object_insert_key_common (ucl_object_t *top, ucl_object_t *elt,
2126 const char *key, size_t keylen, bool copy_key, bool merge, bool replace)
2128 ucl_object_t *found, *tmp;
2129 const ucl_object_t *cur;
2130 ucl_object_iter_t it = NULL;
2134 if (elt == NULL || key == NULL) {
2142 if (top->type != UCL_OBJECT) {
2143 /* It is possible to convert NULL type to an object */
2144 if (top->type == UCL_NULL) {
2145 top->type = UCL_OBJECT;
2148 /* Refuse converting of other object types */
2153 if (top->value.ov == NULL) {
2154 top->value.ov = ucl_hash_create (false);
2158 keylen = strlen (key);
2161 for (p = key; p < key + keylen; p ++) {
2162 if (ucl_test_character (*p, UCL_CHARACTER_UCL_UNSAFE)) {
2163 elt->flags |= UCL_OBJECT_NEED_KEY_ESCAPE;
2168 /* workaround for some use cases */
2169 if (elt->trash_stack[UCL_TRASH_KEY] != NULL &&
2170 key != (const char *)elt->trash_stack[UCL_TRASH_KEY]) {
2171 /* Remove copied key */
2172 free (elt->trash_stack[UCL_TRASH_KEY]);
2173 elt->trash_stack[UCL_TRASH_KEY] = NULL;
2174 elt->flags &= ~UCL_OBJECT_ALLOCATED_KEY;
2178 elt->keylen = keylen;
2181 ucl_copy_key_trash (elt);
2184 found = __DECONST (ucl_object_t *, ucl_hash_search_obj (top->value.ov, elt));
2186 if (found == NULL) {
2187 top->value.ov = ucl_hash_insert_object (top->value.ov, elt, false);
2195 ucl_hash_replace (top->value.ov, found, elt);
2196 ucl_object_unref (found);
2199 if (found->type != UCL_OBJECT && elt->type == UCL_OBJECT) {
2200 /* Insert old elt to new one */
2201 ucl_object_insert_key_common (elt, found, found->key,
2202 found->keylen, copy_key, false, false);
2203 ucl_hash_delete (top->value.ov, found);
2204 top->value.ov = ucl_hash_insert_object (top->value.ov, elt, false);
2206 else if (found->type == UCL_OBJECT && elt->type != UCL_OBJECT) {
2207 /* Insert new to old */
2208 ucl_object_insert_key_common (found, elt, elt->key,
2209 elt->keylen, copy_key, false, false);
2211 else if (found->type == UCL_OBJECT && elt->type == UCL_OBJECT) {
2212 /* Mix two hashes */
2213 while ((cur = ucl_object_iterate (elt, &it, true)) != NULL) {
2214 tmp = ucl_object_ref (cur);
2215 ucl_object_insert_key_common (found, tmp, cur->key,
2216 cur->keylen, copy_key, false, false);
2218 ucl_object_unref (elt);
2221 /* Just make a list of scalars */
2222 DL_APPEND (found, elt);
2226 DL_APPEND (found, elt);
2234 ucl_object_delete_keyl (ucl_object_t *top, const char *key, size_t keylen)
2236 ucl_object_t *found;
2238 if (top == NULL || key == NULL) {
2242 found = __DECONST (ucl_object_t *, ucl_object_lookup_len (top, key, keylen));
2244 if (found == NULL) {
2248 ucl_hash_delete (top->value.ov, found);
2249 ucl_object_unref (found);
2256 ucl_object_delete_key (ucl_object_t *top, const char *key)
2258 return ucl_object_delete_keyl (top, key, strlen (key));
2262 ucl_object_pop_keyl (ucl_object_t *top, const char *key, size_t keylen)
2264 const ucl_object_t *found;
2266 if (top == NULL || key == NULL) {
2269 found = ucl_object_lookup_len (top, key, keylen);
2271 if (found == NULL) {
2274 ucl_hash_delete (top->value.ov, found);
2277 return __DECONST (ucl_object_t *, found);
2281 ucl_object_pop_key (ucl_object_t *top, const char *key)
2283 return ucl_object_pop_keyl (top, key, strlen (key));
2287 ucl_object_insert_key (ucl_object_t *top, ucl_object_t *elt,
2288 const char *key, size_t keylen, bool copy_key)
2290 return ucl_object_insert_key_common (top, elt, key, keylen, copy_key, false, false);
2294 ucl_object_insert_key_merged (ucl_object_t *top, ucl_object_t *elt,
2295 const char *key, size_t keylen, bool copy_key)
2297 return ucl_object_insert_key_common (top, elt, key, keylen, copy_key, true, false);
2301 ucl_object_replace_key (ucl_object_t *top, ucl_object_t *elt,
2302 const char *key, size_t keylen, bool copy_key)
2304 return ucl_object_insert_key_common (top, elt, key, keylen, copy_key, false, true);
2308 ucl_object_merge (ucl_object_t *top, ucl_object_t *elt, bool copy)
2310 ucl_object_t *cur = NULL, *cp = NULL, *found = NULL;
2311 ucl_object_iter_t iter = NULL;
2313 if (top == NULL || top->type != UCL_OBJECT || elt == NULL || elt->type != UCL_OBJECT) {
2317 /* Mix two hashes */
2318 while ((cur = (ucl_object_t*)ucl_hash_iterate (elt->value.ov, &iter))) {
2320 cp = ucl_object_copy (cur);
2323 cp = ucl_object_ref (cur);
2325 found = __DECONST(ucl_object_t *, ucl_hash_search (top->value.ov, cp->key, cp->keylen));
2326 if (found == NULL) {
2327 /* The key does not exist */
2328 top->value.ov = ucl_hash_insert_object (top->value.ov, cp, false);
2332 /* The key already exists, replace it */
2333 ucl_hash_replace (top->value.ov, found, cp);
2334 ucl_object_unref (found);
2341 const ucl_object_t *
2342 ucl_object_lookup_len (const ucl_object_t *obj, const char *key, size_t klen)
2344 const ucl_object_t *ret;
2347 if (obj == NULL || obj->type != UCL_OBJECT || key == NULL) {
2353 ret = ucl_hash_search_obj (obj->value.ov, &srch);
2358 const ucl_object_t *
2359 ucl_object_lookup (const ucl_object_t *obj, const char *key)
2365 return ucl_object_lookup_len (obj, key, strlen (key));
2369 ucl_object_lookup_any (const ucl_object_t *obj,
2370 const char *key, ...)
2373 const ucl_object_t *ret = NULL;
2374 const char *nk = NULL;
2376 if (obj == NULL || key == NULL) {
2380 ret = ucl_object_lookup_len (obj, key, strlen (key));
2385 while (ret == NULL) {
2386 nk = va_arg (ap, const char *);
2392 ret = ucl_object_lookup_len (obj, nk, strlen (nk));
2403 ucl_object_iterate (const ucl_object_t *obj, ucl_object_iter_t *iter, bool expand_values)
2405 const ucl_object_t *elt = NULL;
2407 if (obj == NULL || iter == NULL) {
2411 if (expand_values) {
2412 switch (obj->type) {
2414 return (const ucl_object_t*)ucl_hash_iterate (obj->value.ov, iter);
2418 UCL_ARRAY_GET (vec, obj);
2419 idx = (unsigned int)(uintptr_t)(*iter);
2422 while (idx < kv_size (*vec)) {
2423 if ((elt = kv_A (*vec, idx)) != NULL) {
2429 *iter = (void *)(uintptr_t)idx;
2436 /* Go to linear iteration */
2440 /* Treat everything as a linear list */
2445 else if (elt == obj) {
2448 *iter = __DECONST (void *, elt->next ? elt->next : obj);
2455 const char safe_iter_magic[4] = {'u', 'i', 't', 'e'};
2456 struct ucl_object_safe_iter {
2457 char magic[4]; /* safety check */
2458 const ucl_object_t *impl_it; /* implicit object iteration */
2459 ucl_object_iter_t expl_it; /* explicit iteration */
2462 #define UCL_SAFE_ITER(ptr) (struct ucl_object_safe_iter *)(ptr)
2463 #define UCL_SAFE_ITER_CHECK(it) do { \
2464 assert (it != NULL); \
2465 assert (memcmp (it->magic, safe_iter_magic, sizeof (it->magic)) == 0); \
2469 ucl_object_iterate_new (const ucl_object_t *obj)
2471 struct ucl_object_safe_iter *it;
2473 it = UCL_ALLOC (sizeof (*it));
2475 memcpy (it->magic, safe_iter_magic, sizeof (it->magic));
2480 return (ucl_object_iter_t)it;
2485 ucl_object_iterate_reset (ucl_object_iter_t it, const ucl_object_t *obj)
2487 struct ucl_object_safe_iter *rit = UCL_SAFE_ITER (it);
2489 UCL_SAFE_ITER_CHECK (rit);
2492 rit->expl_it = NULL;
2498 ucl_object_iterate_safe (ucl_object_iter_t it, bool expand_values)
2500 struct ucl_object_safe_iter *rit = UCL_SAFE_ITER (it);
2501 const ucl_object_t *ret = NULL;
2503 UCL_SAFE_ITER_CHECK (rit);
2505 if (rit->impl_it == NULL) {
2509 if (rit->impl_it->type == UCL_OBJECT || rit->impl_it->type == UCL_ARRAY) {
2510 ret = ucl_object_iterate (rit->impl_it, &rit->expl_it, true);
2513 /* Need to switch to another implicit object in chain */
2514 rit->impl_it = rit->impl_it->next;
2515 rit->expl_it = NULL;
2516 return ucl_object_iterate_safe (it, expand_values);
2520 /* Just iterate over the implicit array */
2522 rit->impl_it = rit->impl_it->next;
2523 if (expand_values) {
2524 /* We flatten objects if need to expand values */
2525 if (ret->type == UCL_OBJECT || ret->type == UCL_ARRAY) {
2526 return ucl_object_iterate_safe (it, expand_values);
2535 ucl_object_iterate_free (ucl_object_iter_t it)
2537 struct ucl_object_safe_iter *rit = UCL_SAFE_ITER (it);
2539 UCL_SAFE_ITER_CHECK (rit);
2541 UCL_FREE (sizeof (*rit), it);
2544 const ucl_object_t *
2545 ucl_object_lookup_path (const ucl_object_t *top, const char *path_in) {
2546 return ucl_object_lookup_path_char (top, path_in, '.');
2550 const ucl_object_t *
2551 ucl_object_lookup_path_char (const ucl_object_t *top, const char *path_in, const char sep) {
2552 const ucl_object_t *o = NULL, *found;
2557 if (path_in == NULL || top == NULL) {
2564 /* Skip leading dots */
2570 while (*p != '\0') {
2572 if (*p == sep || *p == '\0') {
2574 switch (top->type) {
2576 /* Key should be an int */
2577 index = strtoul (c, &err_str, 10);
2578 if (err_str != NULL && (*err_str != sep && *err_str != '\0')) {
2581 o = ucl_array_find_index (top, index);
2584 o = ucl_object_lookup_len (top, c, p - c);
2604 ucl_object_new (void)
2606 return ucl_object_typed_new (UCL_NULL);
2610 ucl_object_typed_new (ucl_type_t type)
2612 return ucl_object_new_full (type, 0);
2616 ucl_object_new_full (ucl_type_t type, unsigned priority)
2620 if (type != UCL_USERDATA) {
2621 new = UCL_ALLOC (sizeof (ucl_object_t));
2623 memset (new, 0, sizeof (ucl_object_t));
2625 new->type = (type <= UCL_NULL ? type : UCL_NULL);
2628 ucl_object_set_priority (new, priority);
2630 if (type == UCL_ARRAY) {
2631 new->value.av = UCL_ALLOC (sizeof (ucl_array_t));
2632 if (new->value.av) {
2633 memset (new->value.av, 0, sizeof (ucl_array_t));
2634 UCL_ARRAY_GET (vec, new);
2636 /* Preallocate some space for arrays */
2637 kv_resize (ucl_object_t *, *vec, 8);
2643 new = ucl_object_new_userdata (NULL, NULL, NULL);
2644 ucl_object_set_priority (new, priority);
2651 ucl_object_new_userdata (ucl_userdata_dtor dtor,
2652 ucl_userdata_emitter emitter,
2655 struct ucl_object_userdata *new;
2656 size_t nsize = sizeof (*new);
2658 new = UCL_ALLOC (nsize);
2660 memset (new, 0, nsize);
2662 new->obj.type = UCL_USERDATA;
2663 new->obj.next = NULL;
2664 new->obj.prev = (ucl_object_t *)new;
2666 new->emitter = emitter;
2667 new->obj.value.ud = ptr;
2670 return (ucl_object_t *)new;
2674 ucl_object_type (const ucl_object_t *obj)
2684 ucl_object_fromstring (const char *str)
2686 return ucl_object_fromstring_common (str, 0, UCL_STRING_ESCAPE);
2690 ucl_object_fromlstring (const char *str, size_t len)
2692 return ucl_object_fromstring_common (str, len, UCL_STRING_ESCAPE);
2696 ucl_object_fromint (int64_t iv)
2700 obj = ucl_object_new ();
2702 obj->type = UCL_INT;
2710 ucl_object_fromdouble (double dv)
2714 obj = ucl_object_new ();
2716 obj->type = UCL_FLOAT;
2724 ucl_object_frombool (bool bv)
2728 obj = ucl_object_new ();
2730 obj->type = UCL_BOOLEAN;
2738 ucl_array_append (ucl_object_t *top, ucl_object_t *elt)
2740 UCL_ARRAY_GET (vec, top);
2742 if (elt == NULL || top == NULL) {
2747 vec = UCL_ALLOC (sizeof (*vec));
2754 top->value.av = (void *)vec;
2757 kv_push (ucl_object_t *, *vec, elt);
2765 ucl_array_prepend (ucl_object_t *top, ucl_object_t *elt)
2767 UCL_ARRAY_GET (vec, top);
2769 if (elt == NULL || top == NULL) {
2774 vec = UCL_ALLOC (sizeof (*vec));
2776 top->value.av = (void *)vec;
2777 kv_push (ucl_object_t *, *vec, elt);
2780 /* Slow O(n) algorithm */
2781 kv_prepend (ucl_object_t *, *vec, elt);
2790 ucl_array_merge (ucl_object_t *top, ucl_object_t *elt, bool copy)
2793 ucl_object_t *cp = NULL;
2796 if (elt == NULL || top == NULL || top->type != UCL_ARRAY || elt->type != UCL_ARRAY) {
2801 cp = ucl_object_copy (elt);
2804 cp = ucl_object_ref (elt);
2807 UCL_ARRAY_GET (v1, top);
2808 UCL_ARRAY_GET (v2, cp);
2811 kv_concat (ucl_object_t *, *v1, *v2);
2813 for (i = v2->n; i < v1->n; i ++) {
2814 obj = &kv_A (*v1, i);
2826 ucl_array_delete (ucl_object_t *top, ucl_object_t *elt)
2828 UCL_ARRAY_GET (vec, top);
2829 ucl_object_t *ret = NULL;
2836 for (i = 0; i < vec->n; i ++) {
2837 if (kv_A (*vec, i) == elt) {
2838 kv_del (ucl_object_t *, *vec, i);
2848 const ucl_object_t *
2849 ucl_array_head (const ucl_object_t *top)
2851 UCL_ARRAY_GET (vec, top);
2853 if (vec == NULL || top == NULL || top->type != UCL_ARRAY ||
2854 top->value.av == NULL) {
2858 return (vec->n > 0 ? vec->a[0] : NULL);
2861 const ucl_object_t *
2862 ucl_array_tail (const ucl_object_t *top)
2864 UCL_ARRAY_GET (vec, top);
2866 if (top == NULL || top->type != UCL_ARRAY || top->value.av == NULL) {
2870 return (vec->n > 0 ? vec->a[vec->n - 1] : NULL);
2874 ucl_array_pop_last (ucl_object_t *top)
2876 UCL_ARRAY_GET (vec, top);
2877 ucl_object_t **obj, *ret = NULL;
2879 if (vec != NULL && vec->n > 0) {
2880 obj = &kv_A (*vec, vec->n - 1);
2882 kv_del (ucl_object_t *, *vec, vec->n - 1);
2890 ucl_array_pop_first (ucl_object_t *top)
2892 UCL_ARRAY_GET (vec, top);
2893 ucl_object_t **obj, *ret = NULL;
2895 if (vec != NULL && vec->n > 0) {
2896 obj = &kv_A (*vec, 0);
2898 kv_del (ucl_object_t *, *vec, 0);
2905 const ucl_object_t *
2906 ucl_array_find_index (const ucl_object_t *top, unsigned int index)
2908 UCL_ARRAY_GET (vec, top);
2910 if (vec != NULL && vec->n > 0 && index < vec->n) {
2911 return kv_A (*vec, index);
2918 ucl_array_index_of (ucl_object_t *top, ucl_object_t *elt)
2920 UCL_ARRAY_GET (vec, top);
2924 return (unsigned int)(-1);
2927 for (i = 0; i < vec->n; i ++) {
2928 if (kv_A (*vec, i) == elt) {
2933 return (unsigned int)(-1);
2937 ucl_array_replace_index (ucl_object_t *top, ucl_object_t *elt,
2940 UCL_ARRAY_GET (vec, top);
2941 ucl_object_t *ret = NULL;
2943 if (vec != NULL && vec->n > 0 && index < vec->n) {
2944 ret = kv_A (*vec, index);
2945 kv_A (*vec, index) = elt;
2952 ucl_elt_append (ucl_object_t *head, ucl_object_t *elt)
2961 elt->prev = head->prev;
2962 head->prev->next = elt;
2971 ucl_object_todouble_safe (const ucl_object_t *obj, double *target)
2973 if (obj == NULL || target == NULL) {
2976 switch (obj->type) {
2978 *target = obj->value.iv; /* Probaly could cause overflow */
2982 *target = obj->value.dv;
2992 ucl_object_todouble (const ucl_object_t *obj)
2996 ucl_object_todouble_safe (obj, &result);
3001 ucl_object_toint_safe (const ucl_object_t *obj, int64_t *target)
3003 if (obj == NULL || target == NULL) {
3006 switch (obj->type) {
3008 *target = obj->value.iv;
3012 *target = obj->value.dv; /* Loosing of decimal points */
3022 ucl_object_toint (const ucl_object_t *obj)
3026 ucl_object_toint_safe (obj, &result);
3031 ucl_object_toboolean_safe (const ucl_object_t *obj, bool *target)
3033 if (obj == NULL || target == NULL) {
3036 switch (obj->type) {
3038 *target = (obj->value.iv == true);
3048 ucl_object_toboolean (const ucl_object_t *obj)
3050 bool result = false;
3052 ucl_object_toboolean_safe (obj, &result);
3057 ucl_object_tostring_safe (const ucl_object_t *obj, const char **target)
3059 if (obj == NULL || target == NULL) {
3063 switch (obj->type) {
3065 if (!(obj->flags & UCL_OBJECT_BINARY)) {
3066 *target = ucl_copy_value_trash (obj);
3077 ucl_object_tostring (const ucl_object_t *obj)
3079 const char *result = NULL;
3081 ucl_object_tostring_safe (obj, &result);
3086 ucl_object_tostring_forced (const ucl_object_t *obj)
3088 /* TODO: For binary strings we might encode string here */
3089 if (!(obj->flags & UCL_OBJECT_BINARY)) {
3090 return ucl_copy_value_trash (obj);
3097 ucl_object_tolstring_safe (const ucl_object_t *obj, const char **target, size_t *tlen)
3099 if (obj == NULL || target == NULL) {
3102 switch (obj->type) {
3104 *target = obj->value.sv;
3117 ucl_object_tolstring (const ucl_object_t *obj, size_t *tlen)
3119 const char *result = NULL;
3121 ucl_object_tolstring_safe (obj, &result, tlen);
3126 ucl_object_key (const ucl_object_t *obj)
3128 return ucl_copy_key_trash (obj);
3132 ucl_object_keyl (const ucl_object_t *obj, size_t *len)
3134 if (len == NULL || obj == NULL) {
3142 ucl_object_ref (const ucl_object_t *obj)
3144 ucl_object_t *res = NULL;
3147 if (obj->flags & UCL_OBJECT_EPHEMERAL) {
3149 * Use deep copy for ephemeral objects, note that its refcount
3150 * is NOT increased, since ephemeral objects does not need refcount
3153 res = ucl_object_copy (obj);
3156 res = __DECONST (ucl_object_t *, obj);
3157 #ifdef HAVE_ATOMIC_BUILTINS
3158 (void)__sync_add_and_fetch (&res->ref, 1);
3167 static ucl_object_t *
3168 ucl_object_copy_internal (const ucl_object_t *other, bool allow_array)
3172 ucl_object_iter_t it = NULL;
3173 const ucl_object_t *cur;
3175 new = malloc (sizeof (*new));
3178 memcpy (new, other, sizeof (*new));
3179 if (other->flags & UCL_OBJECT_EPHEMERAL) {
3180 /* Copied object is always non ephemeral */
3181 new->flags &= ~UCL_OBJECT_EPHEMERAL;
3184 /* Unlink from others */
3188 /* deep copy of values stored */
3189 if (other->trash_stack[UCL_TRASH_KEY] != NULL) {
3190 new->trash_stack[UCL_TRASH_KEY] =
3191 strdup (other->trash_stack[UCL_TRASH_KEY]);
3192 if (other->key == (const char *)other->trash_stack[UCL_TRASH_KEY]) {
3193 new->key = new->trash_stack[UCL_TRASH_KEY];
3196 if (other->trash_stack[UCL_TRASH_VALUE] != NULL) {
3197 new->trash_stack[UCL_TRASH_VALUE] =
3198 strdup (other->trash_stack[UCL_TRASH_VALUE]);
3199 if (new->type == UCL_STRING) {
3200 new->value.sv = new->trash_stack[UCL_TRASH_VALUE];
3204 if (other->type == UCL_ARRAY || other->type == UCL_OBJECT) {
3205 /* reset old value */
3206 memset (&new->value, 0, sizeof (new->value));
3208 while ((cur = ucl_object_iterate (other, &it, true)) != NULL) {
3209 if (other->type == UCL_ARRAY) {
3210 ucl_array_append (new, ucl_object_copy_internal (cur, false));
3213 ucl_object_t *cp = ucl_object_copy_internal (cur, true);
3215 ucl_object_insert_key (new, cp, cp->key, cp->keylen,
3221 else if (allow_array && other->next != NULL) {
3222 LL_FOREACH (other->next, cur) {
3223 ucl_object_t *cp = ucl_object_copy_internal (cur, false);
3225 DL_APPEND (new, cp);
3235 ucl_object_copy (const ucl_object_t *other)
3237 return ucl_object_copy_internal (other, true);
3241 ucl_object_unref (ucl_object_t *obj)
3244 #ifdef HAVE_ATOMIC_BUILTINS
3245 unsigned int rc = __sync_sub_and_fetch (&obj->ref, 1);
3248 if (--obj->ref == 0) {
3250 ucl_object_free_internal (obj, true, ucl_object_dtor_unref);
3256 ucl_object_compare (const ucl_object_t *o1, const ucl_object_t *o2)
3258 const ucl_object_t *it1, *it2;
3259 ucl_object_iter_t iter = NULL;
3262 if (o1->type != o2->type) {
3263 return (o1->type) - (o2->type);
3268 if (o1->len == o2->len && o1->len > 0) {
3269 ret = strcmp (ucl_object_tostring(o1), ucl_object_tostring(o2));
3272 ret = o1->len - o2->len;
3278 ret = ucl_object_todouble (o1) - ucl_object_todouble (o2);
3281 ret = ucl_object_toboolean (o1) - ucl_object_toboolean (o2);
3284 if (o1->len == o2->len && o1->len > 0) {
3285 UCL_ARRAY_GET (vec1, o1);
3286 UCL_ARRAY_GET (vec2, o2);
3289 /* Compare all elements in both arrays */
3290 for (i = 0; i < vec1->n; i ++) {
3291 it1 = kv_A (*vec1, i);
3292 it2 = kv_A (*vec2, i);
3294 if (it1 == NULL && it2 != NULL) {
3297 else if (it2 == NULL && it1 != NULL) {
3300 else if (it1 != NULL && it2 != NULL) {
3301 ret = ucl_object_compare (it1, it2);
3309 ret = o1->len - o2->len;
3313 if (o1->len == o2->len && o1->len > 0) {
3314 while ((it1 = ucl_object_iterate (o1, &iter, true)) != NULL) {
3315 it2 = ucl_object_lookup (o2, ucl_object_key (it1));
3320 ret = ucl_object_compare (it1, it2);
3327 ret = o1->len - o2->len;
3339 ucl_object_compare_qsort (const ucl_object_t **o1,
3340 const ucl_object_t **o2)
3342 return ucl_object_compare (*o1, *o2);
3346 ucl_object_array_sort (ucl_object_t *ar,
3347 int (*cmp)(const ucl_object_t **o1, const ucl_object_t **o2))
3349 UCL_ARRAY_GET (vec, ar);
3351 if (cmp == NULL || ar == NULL || ar->type != UCL_ARRAY) {
3355 qsort (vec->a, vec->n, sizeof (ucl_object_t *),
3356 (int (*)(const void *, const void *))cmp);
3362 ucl_object_get_priority (const ucl_object_t *obj)
3368 return (obj->flags >> ((sizeof (obj->flags) * NBBY) - PRIOBITS));
3372 ucl_object_set_priority (ucl_object_t *obj,
3373 unsigned int priority)
3376 priority &= (0x1 << PRIOBITS) - 1;
3377 priority <<= ((sizeof (obj->flags) * NBBY) - PRIOBITS);
3378 priority |= obj->flags & ((1 << ((sizeof (obj->flags) * NBBY) -
3380 obj->flags = priority;
3385 ucl_object_string_to_type (const char *input, ucl_type_t *res)
3387 if (strcasecmp (input, "object") == 0) {
3390 else if (strcasecmp (input, "array") == 0) {
3393 else if (strcasecmp (input, "integer") == 0) {
3396 else if (strcasecmp (input, "number") == 0) {
3399 else if (strcasecmp (input, "string") == 0) {
3402 else if (strcasecmp (input, "boolean") == 0) {
3405 else if (strcasecmp (input, "null") == 0) {
3408 else if (strcasecmp (input, "userdata") == 0) {
3409 *res = UCL_USERDATA;
3419 ucl_object_type_to_string (ucl_type_t type)
3421 const char *res = "unknown";
3454 const ucl_object_t *
3455 ucl_parser_get_comments (struct ucl_parser *parser)
3457 if (parser && parser->comments) {
3458 return parser->comments;
3464 const ucl_object_t *
3465 ucl_comments_find (const ucl_object_t *comments,
3466 const ucl_object_t *srch)
3468 if (comments && srch) {
3469 return ucl_object_lookup_len (comments, (const char *)&srch,
3477 ucl_comments_move (ucl_object_t *comments,
3478 const ucl_object_t *from, const ucl_object_t *to)
3480 const ucl_object_t *found;
3483 if (comments && from && to) {
3484 found = ucl_object_lookup_len (comments,
3485 (const char *)&from, sizeof (void *));
3489 obj = ucl_object_ref (found);
3490 ucl_object_delete_keyl (comments, (const char *)&from,
3492 ucl_object_insert_key (comments, obj, (const char *)&to,
3493 sizeof (void *), true);
3503 ucl_comments_add (ucl_object_t *comments, const ucl_object_t *obj,
3504 const char *comment)
3506 if (comments && obj && comment) {
3507 ucl_object_insert_key (comments, ucl_object_fromstring (comment),
3508 (const char *)&obj, sizeof (void *), true);