const char * pkg_object_dump(const pkg_object *o) { if (o == NULL) return (""); return (ucl_object_emit(o, UCL_EMIT_CONFIG)); }
static void load_repo_file(const char *repofile) { struct ucl_parser *p; ucl_object_t *obj = NULL; bool fallback = false; const char *myarch = NULL; p = ucl_parser_new(0); myarch = pkg_object_string(pkg_config_get("ABI")); ucl_parser_register_variable (p, "ABI", myarch); pkg_debug(1, "PKgConfig: loading %s", repofile); if (!ucl_parser_add_file(p, repofile)) { pkg_emit_error("Error parsing: %s: %s", repofile, ucl_parser_get_error(p)); if (errno == ENOENT) { ucl_parser_free(p); return; } fallback = true; } if (fallback) { obj = yaml_to_ucl(repofile, NULL, 0); if (obj == NULL) return; } if (fallback) { pkg_emit_error("%s file is using a deprecated format. " "Please replace it with the following:\n" "====== BEGIN %s ======\n" "%s" "\n====== END %s ======\n", repofile, repofile, ucl_object_emit(obj, UCL_EMIT_YAML), repofile); } obj = ucl_parser_get_object(p); if (obj->type == UCL_OBJECT) walk_repo_obj(obj, repofile); ucl_object_free(obj); }
static PyObject * ucl_dump (PyObject *self, PyObject *args) { PyObject *obj; ucl_emitter_t emitter; ucl_object_t *root = NULL; emitter = UCL_EMIT_CONFIG; if (!PyArg_ParseTuple(args, "O|i", &obj, &emitter)) { PyErr_SetString(PyExc_TypeError, "Unhandled object type"); return NULL; } if (emitter >= UCL_EMIT_MAX) { PyErr_SetString(PyExc_TypeError, "Invalid emitter type"); return NULL; } if (obj == Py_None) { Py_RETURN_NONE; } if (!PyDict_Check(obj)) { PyErr_SetString(PyExc_TypeError, "Argument must be dict"); return NULL; } root = _iterate_python(obj); if (root) { PyObject *ret; char *buf; buf = (char *) ucl_object_emit (root, emitter); ucl_object_unref (root); #if PY_MAJOR_VERSION < 3 ret = PyString_FromString (buf); #else ret = PyUnicode_FromString (buf); #endif free(buf); return ret; } return NULL; }
static void walk_repo_obj(const ucl_object_t *obj, const char *file, pkg_init_flags flags) { const ucl_object_t *cur; ucl_object_iter_t it = NULL; struct pkg_repo *r; const char *key; while ((cur = ucl_iterate_object(obj, &it, true))) { key = ucl_object_key(cur); pkg_debug(1, "PkgConfig: parsing key '%s'", key); r = pkg_repo_find(key); if (r != NULL) pkg_debug(1, "PkgConfig: overwriting repository %s", key); if (cur->type == UCL_OBJECT) add_repo(cur, r, key, flags); else pkg_emit_error("Ignoring bad configuration entry in %s: %s", file, ucl_object_emit(cur, UCL_EMIT_YAML)); } }
int main (int argc, char **argv) { char inbuf[8192], *test_in = NULL; struct ucl_parser *parser = NULL, *parser2 = NULL; ucl_object_t *obj; FILE *in, *out; unsigned char *emitted = NULL; const char *fname_in = NULL, *fname_out = NULL; int ret = 0, inlen, opt, json = 0, compact = 0, yaml = 0; while ((opt = getopt(argc, argv, "jcy")) != -1) { switch (opt) { case 'j': json = 1; break; case 'c': compact = 1; break; case 'y': yaml = 1; break; default: /* '?' */ fprintf (stderr, "Usage: %s [-jcy] [in] [out]\n", argv[0]); exit (EXIT_FAILURE); } } argc -= optind; argv += optind; switch (argc) { case 1: fname_in = argv[0]; break; case 2: fname_in = argv[0]; fname_out = argv[1]; break; } if (fname_in != NULL) { in = fopen (fname_in, "r"); if (in == NULL) { exit (-errno); } } else { in = stdin; } parser = ucl_parser_new (UCL_PARSER_KEY_LOWERCASE); ucl_parser_register_variable (parser, "ABI", "unknown"); if (fname_in != NULL) { ucl_parser_set_filevars (parser, fname_in, true); } while (!feof (in)) { memset (inbuf, 0, sizeof (inbuf)); if (fread (inbuf, 1, sizeof (inbuf) - 1, in) == 0) { break; } inlen = strlen (inbuf); test_in = malloc (inlen); memcpy (test_in, inbuf, inlen); ucl_parser_add_chunk (parser, test_in, inlen); } fclose (in); if (fname_out != NULL) { out = fopen (fname_out, "w"); if (out == NULL) { exit (-errno); } } else { out = stdout; } if (ucl_parser_get_error (parser) != NULL) { fprintf (out, "Error occurred: %s\n", ucl_parser_get_error(parser)); ret = 1; goto end; } obj = ucl_parser_get_object (parser); if (json) { if (compact) { emitted = ucl_object_emit (obj, UCL_EMIT_JSON_COMPACT); } else { emitted = ucl_object_emit (obj, UCL_EMIT_JSON); } } else if (yaml) { emitted = ucl_object_emit (obj, UCL_EMIT_YAML); } else { emitted = ucl_object_emit (obj, UCL_EMIT_CONFIG); } ucl_parser_free (parser); ucl_object_unref (obj); parser2 = ucl_parser_new (UCL_PARSER_KEY_LOWERCASE); ucl_parser_add_string (parser2, emitted, 0); if (ucl_parser_get_error(parser2) != NULL) { fprintf (out, "Error occurred: %s\n", ucl_parser_get_error(parser2)); fprintf (out, "%s\n", emitted); ret = 1; goto end; } if (emitted != NULL) { free (emitted); } obj = ucl_parser_get_object (parser2); if (json) { if (compact) { emitted = ucl_object_emit (obj, UCL_EMIT_JSON_COMPACT); } else { emitted = ucl_object_emit (obj, UCL_EMIT_JSON); } } else if (yaml) { emitted = ucl_object_emit (obj, UCL_EMIT_YAML); } else { emitted = ucl_object_emit (obj, UCL_EMIT_CONFIG); } fprintf (out, "%s\n", emitted); ucl_object_unref (obj); end: if (emitted != NULL) { free (emitted); } if (parser2 != NULL) { ucl_parser_free (parser2); } if (test_in != NULL) { free (test_in); } fclose (out); return ret; }
int main (int argc, char **argv) { ucl_object_t *obj, *cur, *ar, *ref; const ucl_object_t *found; FILE *out; unsigned char *emitted; const char *fname_out = NULL; int ret = 0; switch (argc) { case 2: fname_out = argv[1]; break; } if (fname_out != NULL) { out = fopen (fname_out, "w"); if (out == NULL) { exit (-errno); } } else { out = stdout; } obj = ucl_object_typed_new (UCL_OBJECT); /* Create some strings */ cur = ucl_object_fromstring_common (" test string ", 0, UCL_STRING_TRIM); ucl_object_insert_key (obj, cur, "key1", 0, false); cur = ucl_object_fromstring_common (" test \nstring\n ", 0, UCL_STRING_TRIM | UCL_STRING_ESCAPE); ucl_object_insert_key (obj, cur, "key2", 0, false); cur = ucl_object_fromstring_common (" test string \n", 0, 0); ucl_object_insert_key (obj, cur, "key3", 0, false); /* Array of numbers */ ar = ucl_object_typed_new (UCL_ARRAY); cur = ucl_object_fromint (10); ucl_array_append (ar, cur); cur = ucl_object_fromdouble (10.1); ucl_array_append (ar, cur); cur = ucl_object_fromdouble (9.999); ucl_array_prepend (ar, cur); /* Removing from an array */ cur = ucl_object_fromdouble (1.0); ucl_array_append (ar, cur); cur = ucl_array_delete (ar, cur); assert (ucl_object_todouble (cur) == 1.0); ucl_object_unref (cur); cur = ucl_object_fromdouble (2.0); ucl_array_append (ar, cur); cur = ucl_array_pop_last (ar); assert (ucl_object_todouble (cur) == 2.0); ucl_object_unref (cur); cur = ucl_object_fromdouble (3.0); ucl_array_prepend (ar, cur); cur = ucl_array_pop_first (ar); assert (ucl_object_todouble (cur) == 3.0); ucl_object_unref (cur); ucl_object_insert_key (obj, ar, "key4", 0, false); cur = ucl_object_frombool (true); /* Ref object to test refcounts */ ref = ucl_object_ref (cur); ucl_object_insert_key (obj, cur, "key4", 0, false); /* Empty strings */ cur = ucl_object_fromstring_common (" ", 0, UCL_STRING_TRIM); ucl_object_insert_key (obj, cur, "key5", 0, false); cur = ucl_object_fromstring_common ("", 0, UCL_STRING_ESCAPE); ucl_object_insert_key (obj, cur, "key6", 0, false); cur = ucl_object_fromstring_common (" \n", 0, UCL_STRING_ESCAPE); ucl_object_insert_key (obj, cur, "key7", 0, false); /* Numbers and booleans */ cur = ucl_object_fromstring_common ("1mb", 0, UCL_STRING_ESCAPE | UCL_STRING_PARSE); ucl_object_insert_key (obj, cur, "key8", 0, false); cur = ucl_object_fromstring_common ("3.14", 0, UCL_STRING_PARSE); ucl_object_insert_key (obj, cur, "key9", 0, false); cur = ucl_object_fromstring_common ("true", 0, UCL_STRING_PARSE); ucl_object_insert_key (obj, cur, "key10", 0, false); cur = ucl_object_fromstring_common (" off ", 0, UCL_STRING_PARSE | UCL_STRING_TRIM); ucl_object_insert_key (obj, cur, "key11", 0, false); cur = ucl_object_fromstring_common ("*****@*****.**", 0, UCL_STRING_PARSE_INT); ucl_object_insert_key (obj, cur, "key12", 0, false); cur = ucl_object_fromstring_common ("#test", 0, UCL_STRING_PARSE_INT); ucl_object_insert_key (obj, cur, "key13", 0, false); cur = ucl_object_frombool (true); ucl_object_insert_key (obj, cur, "k=3", 0, false); /* Try to find using path */ /* Should exist */ found = ucl_lookup_path (obj, "key4.1"); assert (found != NULL && ucl_object_toint (found) == 10); /* . should be ignored */ found = ucl_lookup_path (obj, ".key4.1"); assert (found != NULL && ucl_object_toint (found) == 10); /* moar dots... */ found = ucl_lookup_path (obj, ".key4........1..."); assert (found != NULL && ucl_object_toint (found) == 10); /* No such index */ found = ucl_lookup_path (obj, ".key4.3"); assert (found == NULL); /* No such key */ found = ucl_lookup_path (obj, "key9..key1"); assert (found == NULL); emitted = ucl_object_emit (obj, UCL_EMIT_CONFIG); fprintf (out, "%s\n", emitted); ucl_object_unref (obj); if (emitted != NULL) { free (emitted); } fclose (out); /* Ref should still be accessible */ ref->value.iv = 100500; ucl_object_unref (ref); return ret; }
int main(int argc, char **argv) { char ch; FILE *in = stdin, *out = stdout; const char *schema = NULL; unsigned char *buf = NULL; size_t size = 0, r = 0; struct ucl_parser *parser = NULL; ucl_object_t *obj = NULL; ucl_emitter_t emitter = UCL_EMIT_CONFIG; while((ch = getopt_long(argc, argv, "hi:o:s:f:", opts, NULL)) != -1) { switch (ch) { case 'i': in = fopen(optarg, "r"); if (in == NULL) { perror("fopen on input file"); exit(EXIT_FAILURE); } break; case 'o': out = fopen(optarg, "w"); if (out == NULL) { perror("fopen on output file"); exit(EXIT_FAILURE); } break; case 's': schema = optarg; break; case 'f': if (strcmp(optarg, "ucl") == 0) { emitter = UCL_EMIT_CONFIG; } else if (strcmp(optarg, "json") == 0) { emitter = UCL_EMIT_JSON; } else if (strcmp(optarg, "yaml") == 0) { emitter = UCL_EMIT_YAML; } else if (strcmp(optarg, "compact_json") == 0) { emitter = UCL_EMIT_JSON_COMPACT; } else if (strcmp(optarg, "msgpack") == 0) { emitter = UCL_EMIT_MSGPACK; } else { fprintf(stderr, "Unknown output format: %s\n", optarg); exit(EXIT_FAILURE); } break; case 'h': usage(argv[0], stdout); exit(0); default: usage(argv[0], stderr); exit(EXIT_FAILURE); break; } } parser = ucl_parser_new(0); buf = malloc(BUFSIZ); size = BUFSIZ; while(!feof(in) && !ferror(in)) { if (r == size) { buf = realloc(buf, size*2); size *= 2; if (buf == NULL) { perror("realloc"); exit(EXIT_FAILURE); } } r += fread(buf + r, 1, size - r, in); } if (ferror(in)) { fprintf(stderr, "Failed to read the input file.\n"); exit(EXIT_FAILURE); } fclose(in); if (!ucl_parser_add_chunk(parser, buf, r)) { fprintf(stderr, "Failed to parse input file: %s\n", ucl_parser_get_error(parser)); exit(EXIT_FAILURE); } if ((obj = ucl_parser_get_object(parser)) == NULL) { fprintf(stderr, "Failed to get root object: %s\n", ucl_parser_get_error(parser)); exit(EXIT_FAILURE); } if (schema != NULL) { struct ucl_parser *schema_parser = ucl_parser_new(0); ucl_object_t *schema_obj = NULL; struct ucl_schema_error error; if (!ucl_parser_add_file(schema_parser, schema)) { fprintf(stderr, "Failed to parse schema file: %s\n", ucl_parser_get_error(schema_parser)); exit(EXIT_FAILURE); } if ((schema_obj = ucl_parser_get_object(schema_parser)) == NULL) { fprintf(stderr, "Failed to get root object: %s\n", ucl_parser_get_error(schema_parser)); exit(EXIT_FAILURE); } if (!ucl_object_validate(schema_obj, obj, &error)) { fprintf(stderr, "Validation failed: %s\n", error.msg); exit(EXIT_FAILURE); } } if (emitter != UCL_EMIT_MSGPACK) { fprintf(out, "%s\n", ucl_object_emit(obj, emitter)); } else { size_t len; unsigned char *res; res = ucl_object_emit_len(obj, emitter, &len); fwrite(res, 1, len, out); } return 0; }