- if ((TR_CFG_SUCCESS != tr_cfg_parse_internal(cfg, jcfg)) ||
- (TR_CFG_SUCCESS != tr_cfg_parse_local_orgs(cfg, jcfg)) ||
- (TR_CFG_SUCCESS != tr_cfg_parse_peer_orgs(cfg, jcfg)) ||
- (TR_CFG_SUCCESS != tr_cfg_parse_default_servers(cfg, jcfg)) ||
- (TR_CFG_SUCCESS != tr_cfg_parse_comms(cfg, jcfg)))
+ return jcfg;
+}
+
+/**
+ * Helper to free an array returned by tr_cfg_parse_config_files
+ * @param n_jcfgs
+ * @param jcfgs
+ */
+static void tr_cfg_parse_free_jcfgs(unsigned int n_jcfgs, json_t **jcfgs)
+{
+ int ii=0;
+ for (ii=0; ii<n_jcfgs; ii++)
+ json_decref(jcfgs[ii]);
+ talloc_free(jcfgs);
+}
+
+/**
+ * Parse a list of configuration files. Returns an array of JSON objects, free this with
+ * tr_cfg_parse_free_jcfgs(), a helper function
+ *
+ * @param config_dir
+ * @param n_files
+ * @param cfg_files
+ * @return
+ */
+static json_t **tr_cfg_parse_config_files(TALLOC_CTX *mem_ctx, unsigned int n_files, char **files_with_paths)
+{
+ TALLOC_CTX *tmp_ctx=talloc_new(NULL);
+ 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, unsigned int n_jcfg, json_t **jcfgs, TR_CFG_PARSE_FN parse_fn)
+{
+ size_t ii=0;
+ json_t *this_jcfg=NULL;
+ TR_CFG_RC ret=TR_CFG_ERROR;
+
+ if ((cfg==NULL) || (jcfgs==NULL) || (parse_fn==NULL))