static void load_repo_file(const char *repofile, pkg_init_flags flags) { struct ucl_parser *p; ucl_object_t *obj = NULL; const char *myarch = NULL; const char *myarch_legacy = NULL; p = ucl_parser_new(0); myarch = pkg_object_string(pkg_config_get("ABI")); ucl_parser_register_variable (p, "ABI", myarch); myarch_legacy = pkg_object_string(pkg_config_get("ALTABI")); ucl_parser_register_variable (p, "ALTABI", myarch_legacy); 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)); ucl_parser_free(p); return; } obj = ucl_parser_get_object(p); if (obj == NULL) return; if (obj->type == UCL_OBJECT) walk_repo_obj(obj, repofile, flags); ucl_object_unref(obj); }
static int read_conf_file(const char *confpath, pkg_conf_file_t conftype) { struct ucl_parser *p; ucl_object_t *obj = NULL; p = ucl_parser_new(0); if (!ucl_parser_add_file(p, confpath)) { if (errno != ENOENT) errx(EXIT_FAILURE, "Unable to parse configuration " "file %s: %s", confpath, ucl_parser_get_error(p)); ucl_parser_free(p); /* no configuration present */ return (1); } obj = ucl_parser_get_object(p); if (obj->type != UCL_OBJECT) warnx("Invalid configuration format, ignoring the " "configuration file %s", confpath); else { if (conftype == CONFFILE_PKG) config_parse(obj, conftype); else if (conftype == CONFFILE_REPO) parse_repo_file(obj); } ucl_object_unref(obj); ucl_parser_free(p); return (0); }
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); }
ucl_object_t * vacation_config( char *config_file ) { struct ucl_parser *parser; const char *err; parser = ucl_parser_new( UCL_PARSER_KEY_LOWERCASE ); if ( !ucl_parser_add_file( parser, config_file )) { syslog( LOG_ERR, "vacation_config: UCL parsing failed" ); return( NULL ); } if (( err = ucl_parser_get_error( parser )) != NULL ) { syslog( LOG_ERR, "vacation_config: libucl error: %s", err ); return( NULL ); } return( ucl_parser_get_object( parser )); }
/*** * @function rspamd_cryptobox_keypair.load(file) * Loads public key from UCL file * @param {string} file filename to load * @return {cryptobox_keypair} new keypair */ static gint lua_cryptobox_keypair_load (lua_State *L) { struct rspamd_cryptobox_keypair *kp, **pkp; const gchar *filename; struct ucl_parser *parser; ucl_object_t *obj; filename = luaL_checkstring (L, 1); if (filename != NULL) { parser = ucl_parser_new (0); if (!ucl_parser_add_file (parser, filename)) { msg_err ("cannot open keypair from file: %s, %s", filename, ucl_parser_get_error (parser)); ucl_parser_free (parser); lua_pushnil (L); } else { obj = ucl_parser_get_object (parser); kp = rspamd_keypair_from_ucl (obj); ucl_parser_free (parser); if (kp == NULL) { msg_err ("cannot open keypair from file: %s", filename); ucl_object_unref (obj); lua_pushnil (L); } else { pkp = lua_newuserdata (L, sizeof (gpointer)); *pkp = kp; rspamd_lua_setclass (L, "rspamd{cryptobox_keypair}", -1); ucl_object_unref (obj); } } } else { return luaL_error (L, "bad input arguments"); } return 1; }
int pkg_ini(const char *path, const char *reposdir, pkg_init_flags flags) { struct ucl_parser *p = NULL; size_t i; const char *val = NULL; const char *buf, *walk, *value, *key, *k; const char *evkey = NULL; const char *nsname = NULL; const char *evpipe = NULL; const ucl_object_t *cur, *object; ucl_object_t *obj = NULL, *o, *ncfg; ucl_object_iter_t it = NULL; struct sbuf *ukey = NULL; bool fatal_errors = false; k = NULL; o = NULL; pkg_get_myarch(myabi, BUFSIZ); pkg_get_myarch_legacy(myabi_legacy, BUFSIZ); if (parsed != false) { pkg_emit_error("pkg_init() must only be called once"); return (EPKG_FATAL); } if (((flags & PKG_INIT_FLAG_USE_IPV4) == PKG_INIT_FLAG_USE_IPV4) && ((flags & PKG_INIT_FLAG_USE_IPV6) == PKG_INIT_FLAG_USE_IPV6)) { pkg_emit_error("Invalid flags for pkg_init()"); return (EPKG_FATAL); } config = ucl_object_typed_new(UCL_OBJECT); for (i = 0; i < c_size; i++) { switch (c[i].type) { case PKG_STRING: obj = ucl_object_fromstring_common( c[i].def != NULL ? c[i].def : "", 0, UCL_STRING_TRIM); ucl_object_insert_key(config, obj, c[i].key, strlen(c[i].key), false); break; case PKG_INT: ucl_object_insert_key(config, ucl_object_fromstring_common(c[i].def, 0, UCL_STRING_PARSE_INT), c[i].key, strlen(c[i].key), false); break; case PKG_BOOL: ucl_object_insert_key(config, ucl_object_fromstring_common(c[i].def, 0, UCL_STRING_PARSE_BOOLEAN), c[i].key, strlen(c[i].key), false); break; case PKG_OBJECT: obj = ucl_object_typed_new(UCL_OBJECT); if (c[i].def != NULL) { walk = buf = c[i].def; while ((buf = strchr(buf, ',')) != NULL) { key = walk; value = walk; while (*value != ',') { if (*value == '=') break; value++; } ucl_object_insert_key(obj, ucl_object_fromstring_common(value + 1, buf - value - 1, UCL_STRING_TRIM), key, value - key, false); buf++; walk = buf; } key = walk; value = walk; while (*value != ',') { if (*value == '=') break; value++; } if (o == NULL) o = ucl_object_typed_new(UCL_OBJECT); ucl_object_insert_key(o, ucl_object_fromstring_common(value + 1, strlen(value + 1), UCL_STRING_TRIM), key, value - key, false); } ucl_object_insert_key(config, obj, c[i].key, strlen(c[i].key), false); break; case PKG_ARRAY: obj = ucl_object_typed_new(UCL_ARRAY); if (c[i].def != NULL) { walk = buf = c[i].def; while ((buf = strchr(buf, ',')) != NULL) { ucl_array_append(obj, ucl_object_fromstring_common(walk, buf - walk, UCL_STRING_TRIM)); buf++; walk = buf; } ucl_array_append(obj, ucl_object_fromstring_common(walk, strlen(walk), UCL_STRING_TRIM)); } ucl_object_insert_key(config, obj, c[i].key, strlen(c[i].key), false); break; } } if (path == NULL) path = PREFIX"/etc/pkg.conf"; p = ucl_parser_new(0); errno = 0; obj = NULL; if (!ucl_parser_add_file(p, path)) { if (errno != ENOENT) pkg_emit_error("Invalid configuration file: %s", ucl_parser_get_error(p)); } else { obj = ucl_parser_get_object(p); } ncfg = NULL; while (obj != NULL && (cur = ucl_iterate_object(obj, &it, true))) { sbuf_init(&ukey); key = ucl_object_key(cur); for (i = 0; key[i] != '\0'; i++) sbuf_putc(ukey, toupper(key[i])); sbuf_finish(ukey); object = ucl_object_find_keyl(config, sbuf_data(ukey), sbuf_len(ukey)); if (strncasecmp(sbuf_data(ukey), "PACKAGESITE", sbuf_len(ukey)) == 0 || strncasecmp(sbuf_data(ukey), "PUBKEY", sbuf_len(ukey)) == 0 || strncasecmp(sbuf_data(ukey), "MIRROR_TYPE", sbuf_len(ukey)) == 0) { pkg_emit_error("%s in pkg.conf is no longer " "supported. Convert to the new repository style." " See pkg.conf(5)", sbuf_data(ukey)); fatal_errors = true; continue; } /* ignore unknown keys */ if (object == NULL) continue; if (object->type != cur->type) { pkg_emit_error("Malformed key %s, ignoring", key); continue; } if (ncfg == NULL) ncfg = ucl_object_typed_new(UCL_OBJECT); ucl_object_insert_key(ncfg, ucl_object_copy(cur), sbuf_data(ukey), sbuf_len(ukey), true); } if (fatal_errors) { ucl_object_unref(ncfg); ucl_parser_free(p); return (EPKG_FATAL); } if (ncfg != NULL) { it = NULL; while (( cur = ucl_iterate_object(ncfg, &it, true))) { key = ucl_object_key(cur); ucl_object_replace_key(config, ucl_object_ref(cur), key, strlen(key), true); } ucl_object_unref(ncfg); } ncfg = NULL; it = NULL; while ((cur = ucl_iterate_object(config, &it, true))) { o = NULL; key = ucl_object_key(cur); val = getenv(key); if (val == NULL) continue; switch (cur->type) { case UCL_STRING: o = ucl_object_fromstring_common(val, 0, UCL_STRING_TRIM); break; case UCL_INT: o = ucl_object_fromstring_common(val, 0, UCL_STRING_PARSE_INT); if (o->type != UCL_INT) { pkg_emit_error("Invalid type for environment " "variable %s, got %s, while expecting an integer", key, val); ucl_object_unref(o); continue; } break; case UCL_BOOLEAN: o = ucl_object_fromstring_common(val, 0, UCL_STRING_PARSE_BOOLEAN); if (o->type != UCL_BOOLEAN) { pkg_emit_error("Invalid type for environment " "variable %s, got %s, while expecting a boolean", key, val); ucl_object_unref(o); continue; } break; case UCL_OBJECT: o = ucl_object_typed_new(UCL_OBJECT); walk = buf = val; while ((buf = strchr(buf, ',')) != NULL) { k = walk; value = walk; while (*value != ',') { if (*value == '=') break; value++; } ucl_object_insert_key(o, ucl_object_fromstring_common(value + 1, buf - value - 1, UCL_STRING_TRIM), k, value - k, false); buf++; walk = buf; } key = walk; value = walk; while (*value != '\0') { if (*value == '=') break; value++; } ucl_object_insert_key(o, ucl_object_fromstring_common(value + 1, strlen(value + 1), UCL_STRING_TRIM), k, value - k, false); break; case UCL_ARRAY: o = ucl_object_typed_new(UCL_ARRAY); walk = buf = val; while ((buf = strchr(buf, ',')) != NULL) { ucl_array_append(o, ucl_object_fromstring_common(walk, buf - walk, UCL_STRING_TRIM)); buf++; walk = buf; } ucl_array_append(o, ucl_object_fromstring_common(walk, strlen(walk), UCL_STRING_TRIM)); break; default: /* ignore other types */ break; } if (o != NULL) { if (ncfg == NULL) ncfg = ucl_object_typed_new(UCL_OBJECT); ucl_object_insert_key(ncfg, o, key, strlen(key), true); } } if (ncfg != NULL) { it = NULL; while (( cur = ucl_iterate_object(ncfg, &it, true))) { key = ucl_object_key(cur); ucl_object_replace_key(config, ucl_object_ref(cur), key, strlen(key), true); } ucl_object_unref(ncfg); } disable_plugins_if_static(); parsed = true; ucl_object_unref(obj); ucl_parser_free(p); if (strcmp(pkg_object_string(pkg_config_get("ABI")), "unknown") == 0) { pkg_emit_error("Unable to determine ABI"); return (EPKG_FATAL); } pkg_debug(1, "%s", "pkg initialized"); /* Start the event pipe */ evpipe = pkg_object_string(pkg_config_get("EVENT_PIPE")); if (evpipe != NULL) connect_evpipe(evpipe); debug_level = pkg_object_int(pkg_config_get("DEBUG_LEVEL")); it = NULL; object = ucl_object_find_key(config, "PKG_ENV"); while ((cur = ucl_iterate_object(object, &it, true))) { evkey = ucl_object_key(cur); pkg_debug(1, "Setting env var: %s", evkey); if (evkey != NULL && evkey[0] != '\0') setenv(evkey, ucl_object_tostring_forced(cur), 1); } /* load the repositories */ load_repositories(reposdir, flags); setenv("HTTP_USER_AGENT", "pkg/"PKGVERSION, 1); /* bypass resolv.conf with specified NAMESERVER if any */ nsname = pkg_object_string(pkg_config_get("NAMESERVER")); if (nsname != NULL) set_nameserver(ucl_object_tostring_forced(o)); return (EPKG_OK); }
/** * Entry point. */ int main(int argc, char **argv) { // Parse program options char *config_file = NULL; bool server = false; bool collector = false; bool callibrator = false; bool status_only = false; int log_option = 0; char c; while ((c = getopt(argc, argv, "hc:sdflr")) != EOF) { switch (c) { case 'h': { show_help(argv[0]); return 1; } case 'c': config_file = strdup(optarg); break; case 's': status_only = true; break; case 'd': server = true; break; case 'l': collector = true; break; case 'r': callibrator = true; break; case 'f': log_option |= LOG_PERROR; break; default: { fprintf(stderr, "ERROR: Invalid option %c!\n", c); show_help(argv[0]); return 1; } } } if (config_file == NULL) config_file = strdup("/etc/koruza.cfg"); // Load the configuration file struct ucl_parser *parser = ucl_parser_new(UCL_PARSER_KEY_LOWERCASE); ucl_object_t *config = NULL; ucl_object_t *obj = NULL; int ret_value = 0; if (!parser) { fprintf(stderr, "ERROR: Failed to initialize configuration parser!\n"); return 2; } if (!ucl_parser_add_file(parser, config_file)) { fprintf(stderr, "ERROR: Failed to parse configuration file '%s'!\n", config_file); fprintf(stderr, "ERROR: %s\n", ucl_parser_get_error(parser)); ret_value = 2; goto cleanup_exit; } else { config = ucl_parser_get_object(parser); } if (server) { obj = ucl_object_find_key(config, "server"); if (!obj) { fprintf(stderr, "ERROR: Missing server configuration!\n"); ret_value = 2; goto cleanup_exit; } start_server(obj, log_option); } else if (collector) { start_collector(config, log_option); } else if (callibrator) { start_callibrator(config, log_option); } else { start_controller(config, status_only); } cleanup_exit: // Cleanup and exit if (config) ucl_object_free(config); if (parser) ucl_parser_free(parser); return ret_value; }
static void rspamadm_signtool (gint argc, gchar **argv) { GOptionContext *context; GError *error = NULL; struct ucl_parser *parser; ucl_object_t *top; struct rspamd_cryptobox_pubkey *pk; struct rspamd_cryptobox_keypair *kp; gsize fsize, flen; gint i; context = g_option_context_new ( "keypair - create encryption keys"); g_option_context_set_summary (context, "Summary:\n Rspamd administration utility version " RVERSION "\n Release id: " RID); g_option_context_add_main_entries (context, entries, NULL); if (!g_option_context_parse (context, &argc, &argv, &error)) { rspamd_fprintf (stderr, "option parsing failed: %s\n", error->message); g_error_free (error); exit (1); } if (openssl) { mode = RSPAMD_CRYPTOBOX_MODE_NIST; } if (verify && (!pubkey && !pubkey_file)) { rspamd_fprintf (stderr, "no pubkey for verification\n"); exit (1); } else if (!verify && (!keypair_file)) { rspamd_fprintf (stderr, "no keypair for signing\n"); exit (1); } if (verify) { g_assert (pubkey || pubkey_file); if (pubkey_file) { gint fd; gchar *map; struct stat st; fd = open (pubkey_file, O_RDONLY); if (fd == -1) { rspamd_fprintf (stderr, "cannot open %s: %s\n", pubkey_file, strerror (errno)); exit (errno); } g_assert (fstat (fd, &st) != -1); fsize = st.st_size; flen = fsize; map = mmap (NULL, fsize, PROT_READ, MAP_SHARED, fd, 0); close (fd); if (map == MAP_FAILED) { rspamd_fprintf (stderr, "cannot read %s: %s\n", pubkey_file, strerror (errno)); exit (errno); } /* XXX: assume base32 pubkey now */ while (flen > 0 && g_ascii_isspace (map[flen - 1])) { flen --; } pk = rspamd_pubkey_from_base32 (map, flen, RSPAMD_KEYPAIR_SIGN, mode); if (pk == NULL) { rspamd_fprintf (stderr, "bad size %s: %ud, %ud expected\n", flen, rspamd_cryptobox_pk_sig_bytes (mode)); exit (errno); } munmap (map, fsize); } else { pk = rspamd_pubkey_from_base32 (pubkey, strlen (pubkey), RSPAMD_KEYPAIR_SIGN, mode); if (pk == NULL) { rspamd_fprintf (stderr, "bad size %s: %ud, %ud expected\n", strlen (pubkey), rspamd_cryptobox_pk_sig_bytes (mode)); exit (errno); } } for (i = 1; i < argc; i++) { /* XXX: support cmd line signature */ if (!rspamadm_verify_file (argv[i], rspamd_pubkey_get_pk (pk, NULL))) { exit (EXIT_FAILURE); } } g_free (pk); } else { g_assert (keypair_file != NULL); parser = ucl_parser_new (0); if (!ucl_parser_add_file (parser, keypair_file) || (top = ucl_parser_get_object (parser)) == NULL) { rspamd_fprintf (stderr, "cannot load keypair: %s\n", ucl_parser_get_error (parser)); exit (EINVAL); } ucl_parser_free (parser); kp = rspamd_keypair_from_ucl (top); if (kp == NULL || rspamd_keypair_type (kp) != RSPAMD_KEYPAIR_SIGN) { rspamd_fprintf (stderr, "invalid or unsuitable for signing key\n"); exit (EXIT_FAILURE); } for (i = 1; i < argc; i++) { /* XXX: support cmd line signature */ if (!rspamadm_sign_file (argv[i], kp)) { rspamd_keypair_unref (kp); exit (EXIT_FAILURE); } } rspamd_keypair_unref (kp); } }
static void rspamadm_statconvert (gint argc, gchar **argv, const struct rspamadm_command *cmd) { GOptionContext *context; GError *error = NULL; ucl_object_t *obj; context = g_option_context_new ( "statconvert - converts statistics from sqlite3 to redis"); g_option_context_set_summary (context, "Summary:\n Rspamd administration utility version " RVERSION "\n Release id: " RID); g_option_context_add_main_entries (context, entries, NULL); g_option_context_set_ignore_unknown_options (context, TRUE); if (!g_option_context_parse (context, &argc, &argv, &error)) { rspamd_fprintf (stderr, "option parsing failed: %s\n", error->message); g_error_free (error); exit (1); } if (config_file) { /* Load config file, assuming that it has all information required */ struct ucl_parser *parser; parser = ucl_parser_new (0); rspamd_ucl_add_conf_variables (parser, ucl_vars); if (!ucl_parser_add_file (parser, config_file)) { msg_err ("ucl parser error: %s", ucl_parser_get_error (parser)); ucl_parser_free (parser); exit (EXIT_FAILURE); } obj = ucl_parser_get_object (parser); ucl_parser_free (parser); } else { /* We need to get all information from the command line */ ucl_object_t *classifier, *statfile_ham, *statfile_spam, *tmp, *redis; /* Check arguments sanity */ if (spam_db == NULL) { msg_err ("No spam-db specified"); exit (EXIT_FAILURE); } if (ham_db == NULL) { msg_err ("No ham-db specified"); exit (EXIT_FAILURE); } if (redis_host == NULL) { msg_err ("No redis-host specified"); exit (EXIT_FAILURE); } if (symbol_ham == NULL) { msg_err ("No symbol-ham specified"); exit (EXIT_FAILURE); } if (symbol_spam == NULL) { msg_err ("No symbol-spam specified"); exit (EXIT_FAILURE); } obj = ucl_object_typed_new (UCL_OBJECT); classifier = ucl_object_typed_new (UCL_OBJECT); ucl_object_insert_key (obj, classifier, "classifier", 0, false); /* Now we need to create "bayes" key in it */ tmp = ucl_object_typed_new (UCL_OBJECT); ucl_object_insert_key (classifier, tmp, "bayes", 0, false); classifier = tmp; ucl_object_insert_key (classifier, ucl_object_fromstring ("sqlite3"), "backend", 0, false); if (cache_db != NULL) { ucl_object_t *cache; cache = ucl_object_typed_new (UCL_OBJECT); ucl_object_insert_key (cache, ucl_object_fromstring ("sqlite3"), "type", 0, false); ucl_object_insert_key (cache, ucl_object_fromstring (cache_db), "file", 0, false); ucl_object_insert_key (classifier, cache, "cache", 0, false); } statfile_ham = ucl_object_typed_new (UCL_OBJECT); ucl_object_insert_key (statfile_ham, ucl_object_fromstring (symbol_ham), "symbol", 0, false); ucl_object_insert_key (statfile_ham, ucl_object_frombool (false), "spam", 0, false); ucl_object_insert_key (statfile_ham, ucl_object_fromstring (ham_db), "db", 0, false); statfile_spam = ucl_object_typed_new (UCL_OBJECT); ucl_object_insert_key (statfile_spam, ucl_object_fromstring (symbol_spam), "symbol", 0, false); ucl_object_insert_key (statfile_spam, ucl_object_frombool (true), "spam", 0, false); ucl_object_insert_key (statfile_spam, ucl_object_fromstring (spam_db), "db", 0, false); DL_APPEND (statfile_ham, statfile_spam); ucl_object_insert_key (classifier, statfile_ham, "statfile", 0, false); /* Deal with redis */ redis = ucl_object_typed_new (UCL_OBJECT); ucl_object_insert_key (obj, redis, "redis", 0, false); ucl_object_insert_key (redis, ucl_object_fromstring (redis_host), "servers", 0, false); if (redis_db) { ucl_object_insert_key (redis, ucl_object_fromstring (redis_db), "dbname", 0, false); } if (redis_password) { ucl_object_insert_key (redis, ucl_object_fromstring (redis_password), "password", 0, false); } } ucl_object_insert_key (obj, ucl_object_frombool (reset_previous), "reset_previous", 0, false); if (expire != 0) { ucl_object_insert_key (obj, ucl_object_fromdouble (expire), "expire", 0, false); } rspamadm_execute_lua_ucl_subr (argc, argv, obj, "stat_convert", TRUE); ucl_object_unref (obj); }
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; }