+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)
+{
+ char *p = s;
+
+ for (p = s; *p; p++)
+ {
+ if (!isspace(*(unsigned char *)p))
+ return 0;
+ }
+ return 1;
+}
+
+static void parse_data1_tree(struct grs_read_info *p, const char *mc_stmnt,
+ data1_node *root)
+{
+ data1_marctab *marctab = data1_absyn_getmarctab(p->dh, root->u.root.absyn);
+ data1_node *top = root->child;
+ data1_node *field;
+ mc_context *c;
+ mc_field *pf;
+ WRBUF buf;
+
+ c = mc_mk_context(mc_stmnt+3);
+
+ if (!c)
+ return;
+
+ pf = mc_getfield(c);
+
+ if (!pf)
+ {
+ mc_destroy_context(c);
+ return;
+ }
+ buf = wrbuf_alloc();
+#if MARCOMP_DEBUG
+ yaz_log(YLOG_LOG, "parse_data1_tree(): statement -{%s}", mc_stmnt);
+#endif
+ if (!yaz_matchstr(pf->name, "ldr"))
+ {
+ data1_node *new;
+#if MARCOMP_DEBUG
+ yaz_log(YLOG_LOG,"parse_data1_tree(): try LEADER from {%d} to {%d} positions",
+ pf->interval.start, pf->interval.end);
+#endif
+ new = data1_mk_tag_n(p->dh, p->mem, mc_stmnt, strlen(mc_stmnt), 0, top);
+ data1_mk_text_n(p->dh, p->mem, marctab->leader+pf->interval.start,
+ pf->interval.end-pf->interval.start+1, new);
+ }
+ else
+ {
+ field=top->child;
+
+ while(field)
+ {
+ if (!yaz_matchstr(field->u.tag.tag, pf->name))
+ {
+ data1_node *new;
+ char *pb;
+#if MARCOMP_DEBUG
+ yaz_log(YLOG_LOG, "parse_data1_tree(): try field {%s}", field->u.tag.tag);
+#endif
+ wrbuf_rewind(buf);
+ wrbuf_puts(buf, "");
+
+ field = cat_field(p, pf, buf, field);
+
+ pb = wrbuf_buf(buf);
+ for (pb = strtok(pb, "\n"); pb; pb = strtok(NULL, "\n"))
+ {
+ if (!is_empty(pb))
+ {
+ new = data1_mk_tag_n(p->dh, p->mem, mc_stmnt, strlen(mc_stmnt), 0, top);
+ data1_mk_text_n(p->dh, p->mem, pb, strlen(pb), new);
+ }
+ }
+ }
+ else
+ {
+ field = field->next;
+ }
+ }
+ }
+ mc_destroy_field(pf);
+ mc_destroy_context(c);
+ wrbuf_free(buf, 1);
+}
+
+data1_node *grs_read_marcxml(struct grs_read_info *p)
+{
+ data1_node *root = grs_read_iso2709(p, 1);
+ data1_element *e;
+
+ if (!root)
+ return 0;
+
+ for (e = data1_absyn_getelements(p->dh, root->u.root.absyn); e; e=e->next)
+ {
+ data1_tag *tag = e->tag;
+
+ if (tag && tag->which == DATA1T_string &&
+ !yaz_matchstr(tag->value.string, "mc?"))
+ parse_data1_tree(p, tag->value.string, root);
+ }
+ return root;
+}
+
+data1_node *grs_read_marc(struct grs_read_info *p)
+{
+ data1_node *root = grs_read_iso2709(p, 0);
+ data1_element *e;
+
+ if (!root)
+ return 0;
+
+ for (e = data1_absyn_getelements(p->dh, root->u.root.absyn); e; e=e->next)
+ {
+ data1_tag *tag = e->tag;
+
+ if (tag && tag->which == DATA1T_string &&
+ !yaz_matchstr(tag->value.string, "mc?"))
+ parse_data1_tree(p, tag->value.string, root);
+ }
+ return root;
+}
+
+static void *init_marc(Res res, RecType rt)
+{
+ struct marc_info *p = xmalloc(sizeof(*p));
+ strcpy(p->type, "");
+ return p;
+}
+
+static void config_marc(void *clientData, Res res, const char *args)
+{
+ struct marc_info *p = (struct marc_info*) clientData;
+ if (strlen(args) < sizeof(p->type))
+ strcpy(p->type, args);
+}
+
+static void destroy_marc(void *clientData)
+{
+ struct marc_info *p = (struct marc_info*) clientData;
+ xfree (p);
+}
+
+
+static int extract_marc(void *clientData, struct recExtractCtrl *ctrl)
+{
+ return zebra_grs_extract(clientData, ctrl, grs_read_marc);
+}
+
+static int retrieve_marc(void *clientData, struct recRetrieveCtrl *ctrl)
+{
+ return zebra_grs_retrieve(clientData, ctrl, grs_read_marc);
+}
+
+static struct recType marc_type = {
+ 0,
+ "grs.marc",
+ init_marc,
+ config_marc,
+ destroy_marc,
+ extract_marc,
+ retrieve_marc,