+static inline_subfield *lookup_inline_subfield(inline_subfield *pisf,
+ const char *name)
+{
+ inline_subfield *p;
+
+ for (p=pisf; p; p=p->next)
+ {
+ if (!yaz_matchstr(p->name, name))
+ return p;
+ }
+ return 0;
+}
+
+static inline_subfield *cat_inline_subfield(mc_subfield *psf, WRBUF buf,
+ inline_subfield *pisf)
+{
+ mc_subfield *p;
+
+ for (p = psf; p && pisf; p = p->next)
+ {
+ if (p->which == MC_SF)
+ {
+ inline_subfield *found = lookup_inline_subfield(pisf, p->name);
+
+ if (found)
+ {
+ if (strcmp(p->prefix, "_"))
+ {
+ wrbuf_puts(buf, " ");
+ wrbuf_puts(buf, p->prefix);
+ }
+ if (p->interval.start == -1)
+ {
+ wrbuf_puts(buf, found->data);
+ }
+ else
+ {
+ wrbuf_write(buf, found->data+p->interval.start,
+ p->interval.end-p->interval.start);
+ wrbuf_puts(buf, "");
+ }
+ if (strcmp(p->suffix, "_"))
+ {
+ wrbuf_puts(buf, p->suffix);
+ wrbuf_puts(buf, " ");
+ }
+#if MARCOMP_DEBUG
+ yaz_log(YLOG_LOG, "cat_inline_subfield(): add subfield $%s", found->name);
+#endif
+ pisf = found->next;
+ }
+ }
+ else if (p->which == MC_SFVARIANT)
+ {
+ inline_subfield *next;
+
+ do {
+ next = cat_inline_subfield(p->u.child, buf, pisf);
+ if (next == pisf)
+ break;
+ pisf = next;
+ } while (pisf);
+ }
+ else if (p->which == MC_SFGROUP)
+ {
+ mc_subfield *pp;
+ int found;
+
+ for (pp = p->u.child, found = 0; pp; pp = pp->next)
+ {
+ if (!yaz_matchstr(pisf->name, p->name))
+ {
+ found = 1;
+ break;
+ }
+ }
+ if (found)
+ {
+ wrbuf_puts(buf, " (");
+ pisf = cat_inline_subfield(p->u.child, buf, pisf);
+ wrbuf_puts(buf, ") ");
+ }
+ }
+ }
+ return pisf;
+}
+
+static void cat_inline_field(mc_field *pf, WRBUF buf, data1_node *subfield)
+{
+ if (!pf || !subfield)
+ return;
+
+ for (;subfield;)
+ {
+ int len;
+ inline_field *pif=NULL;
+ data1_node *psubf;
+
+ if (yaz_matchstr(subfield->u.tag.tag, "1"))
+ {
+ subfield = subfield->next;
+ continue;
+ }
+
+ psubf = subfield;
+ pif = inline_mk_field();
+ do
+ {
+ int i;
+ if ((i=inline_parse(pif, psubf->u.tag.tag, get_data(psubf, &len)))<0)
+ {
+ yaz_log(YLOG_WARN, "inline subfield ($%s): parse error",
+ psubf->u.tag.tag);
+ inline_destroy_field(pif);
+ return;
+ }
+ psubf = psubf->next;
+ } while (psubf && yaz_matchstr(psubf->u.tag.tag, "1"));
+
+ subfield = psubf;
+
+ if (pif && !yaz_matchstr(pif->name, pf->name))
+ {
+ if (!pf->list && pif->list)
+ {
+ wrbuf_puts(buf, pif->list->data);
+ }
+ else
+ {
+ int ind1, ind2;
+
+ /*
+ check indicators
+ */
+
+ ind1 = (pif->ind1[0] == ' ') ? '_':pif->ind1[0];
+ ind2 = (pif->ind2[0] == ' ') ? '_':pif->ind2[0];
+
+ if (((pf->ind1[0] == '.') || (ind1 == pf->ind1[0])) &&
+ ((pf->ind2[0] == '.') || (ind2 == pf->ind2[0])))
+ {
+ cat_inline_subfield(pf->list, buf, pif->list);
+
+ /*
+ add separator for inline fields
+ */
+ if (wrbuf_len(buf))
+ {
+ wrbuf_puts(buf, "\n");
+ }
+ }
+ else
+ {
+ yaz_log(YLOG_WARN, "In-line field %s missed -- indicators do not match", pif->name);
+ }
+ }
+ }
+ inline_destroy_field(pif);
+ }
+#if MARCOMP_DEBUG
+ yaz_log(YLOG_LOG, "cat_inline_field(): got buffer {%s}", buf);
+#endif
+}
+
+static data1_node *cat_subfield(mc_subfield *psf, WRBUF buf,
+ data1_node *subfield)
+{
+ mc_subfield *p;
+
+ for (p = psf; p && subfield; p = p->next)
+ {
+ if (p->which == MC_SF)
+ {
+ data1_node *found = lookup_subfield(subfield, p->name);
+
+ if (found)
+ {
+ int len;
+
+ if (strcmp(p->prefix, "_"))
+ {
+ wrbuf_puts(buf, " ");
+ wrbuf_puts(buf, p->prefix);
+ }
+
+ if (p->u.in_line)
+ {
+ cat_inline_field(p->u.in_line, buf, found);
+ }
+ else if (p->interval.start == -1)
+ {
+ wrbuf_puts(buf, get_data(found, &len));
+ }
+ else
+ {
+ wrbuf_write(buf, get_data(found, &len)+p->interval.start,
+ p->interval.end-p->interval.start);
+ wrbuf_puts(buf, "");
+ }
+ if (strcmp(p->suffix, "_"))
+ {
+ wrbuf_puts(buf, p->suffix);
+ wrbuf_puts(buf, " ");
+ }
+#if MARCOMP_DEBUG
+ yaz_log(YLOG_LOG, "cat_subfield(): add subfield $%s", found->u.tag.tag);
+#endif
+ subfield = found->next;
+ }
+ }
+ else if (p->which == MC_SFVARIANT)
+ {
+ data1_node *next;
+ do {
+ next = cat_subfield(p->u.child, buf, subfield);
+ if (next == subfield)
+ break;
+ subfield = next;
+ } while (subfield);
+ }
+ else if (p->which == MC_SFGROUP)
+ {
+ mc_subfield *pp;
+ int found;
+
+ for (pp = p->u.child, found = 0; pp; pp = pp->next)
+ {
+ if (!yaz_matchstr(subfield->u.tag.tag, pp->name))
+ {
+ found = 1;
+ break;
+ }
+ }
+ if (found)
+ {
+ wrbuf_puts(buf, " (");
+ subfield = cat_subfield(p->u.child, buf, subfield);
+ wrbuf_puts(buf, ") ");
+ }
+ }
+ }
+ return subfield;
+}
+
+static data1_node *cat_field(struct grs_read_info *p, mc_field *pf,
+ WRBUF buf, data1_node *field)
+{
+ data1_node *subfield;
+ int ind1, ind2;
+
+ if (!pf || !field)
+ return 0;
+
+
+ if (yaz_matchstr(field->u.tag.tag, pf->name))
+ return field->next;
+
+ subfield = field->child;
+
+ if (!subfield)
+ return field->next;
+
+ /*
+ check subfield without indicators
+ */
+
+ if (!pf->list && subfield->which == DATA1N_data)
+ {
+ int len;
+
+ if (pf->interval.start == -1)
+ {
+ wrbuf_puts(buf, get_data(field, &len));
+ }
+ else
+ {
+ wrbuf_write(buf, get_data(field, &len)+pf->interval.start,
+ pf->interval.end-pf->interval.start);
+ wrbuf_puts(buf, "");
+ }
+#if MARCOMP_DEBUG
+ yaz_log(YLOG_LOG, "cat_field(): got buffer {%s}", buf);
+#endif
+ return field->next;
+ }
+
+ /*
+ check indicators
+ */
+
+ ind1 = (subfield->u.tag.tag[0] == ' ') ? '_':subfield->u.tag.tag[0];
+ ind2 = (subfield->u.tag.tag[1] == ' ') ? '_':subfield->u.tag.tag[1];
+
+ if (!(
+ ((pf->ind1[0] == '.') || (ind1 == pf->ind1[0])) &&
+ ((pf->ind2[0] == '.') || (ind2 == pf->ind2[0]))
+ ))
+ {
+#if MARCOMP_DEBUG
+ yaz_log(YLOG_WARN, "Field %s missed -- does not match indicators", field->u.tag.tag);
+#endif
+ return field->next;
+ }
+
+ subfield = subfield->child;
+
+ if (!subfield)
+ return field->next;
+
+ cat_subfield(pf->list, buf, subfield);
+
+#if MARCOMP_DEBUG
+ yaz_log(YLOG_LOG, "cat_field(): got buffer {%s}", buf);
+#endif
+
+ return field->next;
+}
+
+static int is_empty(char *s)