+ unsigned int ii=0;
+ json_t **jcfgs=NULL;
+
+ /* first allocate the jcfgs */
+ jcfgs=talloc_array(NULL, json_t *, n_files);
+ if (jcfgs==NULL) {
+ tr_crit("tr_parse_config_files: cannot allocate JSON structure array");
+ goto cleanup;
+ }
+ for (ii=0; ii<n_files; ii++) {
+ jcfgs[ii]=tr_cfg_parse_one_config_file(files_with_paths[ii]);
+ if (jcfgs[ii]==NULL) {
+ tr_err("tr_parse_config: Error parsing JSON in %s", files_with_paths[ii]);
+ tr_cfg_parse_free_jcfgs(ii, jcfgs); /* frees the JSON objects and the jcfgs array */
+ jcfgs=NULL;
+ goto cleanup;
+ }
+ }
+cleanup:
+ if (jcfgs)
+ talloc_steal(mem_ctx, jcfgs); /* give this to the caller's context if we succeeded */
+ talloc_free(tmp_ctx);
+ return jcfgs;
+}
+
+/* define a type for config parse functions */
+typedef TR_CFG_RC (TR_CFG_PARSE_FN)(TR_CFG *, json_t *);
+/**
+ * Helper function to parse a collection of JSON structures using a generic parse function.
+ *
+ * @param cfg Config structure to receive results
+ * @param jcfgs Pointer to an array of decoded JSON structures
+ * @param n_jcfg Number of JSON structures in the array
+ * @param parse_fn Function to apply
+ * @return TR_CFG_SUCCESS on success, _FAIL or an error code on failure
+ */
+static TR_CFG_RC tr_cfg_parse_helper(TR_CFG *cfg, json_t **jcfgs, size_t n_jcfg, TR_CFG_PARSE_FN parse_fn)
+{
+ size_t ii=0;
+ json_t *this_jcfg=NULL;
+ TR_CFG_RC ret=TR_CFG_ERROR;
+
+ /* TODO validate arguments */
+
+ for (ii=0; ii<n_jcfg; ii++) {
+ this_jcfg=jcfgs[ii];
+ ret=parse_fn(cfg, this_jcfg);
+ if (ret!=TR_CFG_SUCCESS)
+ break;
+ }
+ return ret;
+}
+
+
+/**
+ * Reads configuration files in config_dir ("" or "./" will use the current directory).
+ *
+ * @param cfg_mgr Configuration manager
+ * @param n_files Number of entries in cfg_files
+ * @param files_with_paths Array of filenames with path to load
+ * @return TR_CFG_SUCCESS on success, TR_CFG_ERROR or a more specific error on failure
+ */
+TR_CFG_RC tr_parse_config(TR_CFG_MGR *cfg_mgr, unsigned int n_files, char **files_with_paths)
+{
+ TALLOC_CTX *tmp_ctx=talloc_new(NULL);
+ json_t **jcfgs=NULL;