+// Initialize YAZ Map structures for MARC-based targets
+static int prepare_yazmarc(struct session_database *sdb)
+{
+ char *s;
+
+ if (!sdb->settings)
+ {
+ yaz_log(YLOG_WARN, "No settings for %s", sdb->database->url);
+ return -1;
+ }
+ if ((s = session_setting_oneval(sdb, PZ_NATIVESYNTAX)) && !strncmp(s, "iso2709", 7))
+ {
+ char *encoding = "marc-8s", *e;
+ yaz_iconv_t cm;
+
+ // See if a native encoding is specified
+ if ((e = strchr(s, ';')))
+ encoding = e + 1;
+
+ sdb->yaz_marc = yaz_marc_create();
+ yaz_marc_subfield_str(sdb->yaz_marc, "\t");
+
+ cm = yaz_iconv_open("utf-8", encoding);
+ if (!cm)
+ {
+ yaz_log(YLOG_FATAL,
+ "Unable to map from %s to UTF-8 for target %s",
+ encoding, sdb->database->url);
+ return -1;
+ }
+ yaz_marc_iconv(sdb->yaz_marc, cm);
+ }
+ return 0;
+}
+
+// Prepare XSLT stylesheets for record normalization
+// Structures are allocated on the session_wide nmem to avoid having
+// to recompute this for every search. This would lead
+// to leaking if a single session was to repeatedly change the PZ_XSLT
+// setting. However, this is not a realistic use scenario.
+static int prepare_map(struct session *se, struct session_database *sdb)
+{
+ char *s;
+
+ if (!sdb->settings)
+ {
+ yaz_log(YLOG_WARN, "No settings on %s", sdb->database->url);
+ return -1;
+ }
+ if ((s = session_setting_oneval(sdb, PZ_XSLT)))
+ {
+ char **stylesheets;
+ struct database_retrievalmap **m = &sdb->map;
+ int num, i;
+
+ nmem_strsplit(se->session_nmem, ",", s, &stylesheets, &num);
+ for (i = 0; i < num; i++)
+ {
+ (*m) = nmem_malloc(se->session_nmem, sizeof(**m));
+ (*m)->next = 0;
+ if (!((*m)->stylesheet = conf_load_stylesheet(stylesheets[i])))
+ {
+ yaz_log(YLOG_FATAL, "Unable to load stylesheet: %s",
+ stylesheets[i]);
+ return -1;
+ }
+ m = &(*m)->next;
+ }
+ }
+ if (!sdb->map)
+ yaz_log(YLOG_WARN, "No Normalization stylesheet for target %s",
+ sdb->database->url);
+ return 0;
+}
+
+// This analyzes settings and recomputes any supporting data structures
+// if necessary.
+static int prepare_session_database(struct session *se, struct session_database *sdb)
+{
+ if (!sdb->settings)
+ {
+ yaz_log(YLOG_WARN, "No settings associated with %s", sdb->database->url);
+ return -1;
+ }
+ if (sdb->settings[PZ_NATIVESYNTAX] && !sdb->yaz_marc)
+ {
+ if (prepare_yazmarc(sdb) < 0)
+ return -1;
+ }
+ if (sdb->settings[PZ_XSLT] && !sdb->map)
+ {
+ if (prepare_map(se, sdb) < 0)
+ return -1;
+ }
+ return 0;
+}
+
+// Initialize CCL map for a target
+static CCL_bibset prepare_cclmap(struct client *cl)
+{
+ struct session_database *sdb = cl->database;
+ struct setting *s;
+ CCL_bibset res;
+
+ if (!sdb->settings)
+ return 0;
+ res = ccl_qual_mk();
+ for (s = sdb->settings[PZ_CCLMAP]; s; s = s->next)
+ {
+ char *p = strchr(s->name + 3, ':');
+ if (!p)
+ {
+ yaz_log(YLOG_WARN, "Malformed cclmap name: %s", s->name);
+ ccl_qual_rm(&res);
+ return 0;
+ }
+ p++;
+ ccl_qual_fitem(res, s->value, p);
+ }
+ return res;
+}
+