1 /* $Id: marcread.c,v 1.33 2006-04-26 11:12:31 adam Exp $
2 Copyright (C) 1995-2005
5 This file is part of the Zebra server.
7 Zebra is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with Zebra; see the file LICENSE.zebra. If not, write to the
19 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
27 #include <yaz/yaz-util.h>
28 #include <yaz/marcdisp.h>
29 #include <idzebra/recgrs.h>
34 #define MARCOMP_DEBUG 0
40 static data1_node *grs_read_iso2709 (struct grs_read_info *p, int marc_xml)
42 struct marc_info *mi = (struct marc_info*) p->clientData;
47 int identifier_length;
50 int length_data_entry;
52 int length_implementation;
57 data1_node *res_root, *res_top;
59 data1_marctab *marctab;
61 if ((*p->readf)(p->fh, buf, 5) != 5)
63 while (*buf < '0' || *buf > '9')
67 yaz_log(YLOG_WARN, "MARC: Skipping bad byte %d (0x%02X)",
68 *buf & 0xff, *buf & 0xff);
72 if ((*p->readf)(p->fh, buf+4, 1) != 1)
75 record_length = atoi_n (buf, 5);
76 if (record_length < 25)
78 yaz_log (YLOG_WARN, "MARC record length < 25, is %d", record_length);
81 /* read remaining part - attempt to read one byte furhter... */
82 read_bytes = (*p->readf)(p->fh, buf+5, record_length-4);
83 if (read_bytes < record_length-5)
85 yaz_log (YLOG_WARN, "Couldn't read whole MARC record");
88 if (read_bytes == record_length - 4)
90 off_t cur_offset = (*p->tellf)(p->fh);
94 (*p->endf)(p->fh, cur_offset - 1);
97 res_root = data1_mk_root (p->dh, p->mem, absynName);
100 yaz_log (YLOG_WARN, "cannot read MARC without an abstract syntax");
106 const char *attr[] = { "xmlns", "http://www.loc.gov/MARC21/slim", 0};
108 res_top = data1_mk_tag (p->dh, p->mem, "record", attr, res_root);
110 lead = data1_mk_tag(p->dh, p->mem, "leader", 0, res_top);
111 data1_mk_text_n(p->dh, p->mem, buf, 24, lead);
114 res_top = data1_mk_tag (p->dh, p->mem, absynName, 0, res_root);
116 if ((marctab = data1_absyn_getmarctab(p->dh, res_root->u.root.absyn)))
118 memcpy(marctab->leader, buf, 24);
119 memcpy(marctab->implementation_codes, buf+6, 4);
120 marctab->implementation_codes[4] = '\0';
121 memcpy(marctab->user_systems, buf+17, 3);
122 marctab->user_systems[3] = '\0';
125 if (marctab && marctab->force_indicator_length >= 0)
126 indicator_length = marctab->force_indicator_length;
128 indicator_length = atoi_n (buf+10, 1);
129 if (marctab && marctab->force_identifier_length >= 0)
130 identifier_length = marctab->force_identifier_length;
132 identifier_length = atoi_n (buf+11, 1);
133 base_address = atoi_n (buf+12, 5);
135 length_data_entry = atoi_n (buf+20, 1);
136 length_starting = atoi_n (buf+21, 1);
137 length_implementation = atoi_n (buf+22, 1);
139 for (entry_p = 24; buf[entry_p] != ISO2709_FS; )
141 int l = 3 + length_data_entry + length_starting;
142 if (entry_p + l >= record_length)
144 yaz_log(YLOG_WARN, "MARC: Directory offset %d: end of record.",
148 /* check for digits in length info */
150 if (!isdigit(*(const unsigned char *) (buf + entry_p+l)))
154 /* not all digits, so stop directory scan */
155 yaz_log(YLOG_LOG, "MARC: Bad directory");
158 entry_p += 3 + length_data_entry + length_starting;
160 end_of_directory = entry_p;
161 if (base_address != entry_p+1)
163 yaz_log(YLOG_WARN, "MARC: Base address does not follow directory");
165 for (entry_p = 24; entry_p != end_of_directory; )
173 data1_node *parent = res_top;
175 memcpy (tag, buf+entry_p, 3);
182 res = data1_mk_tag_n (p->dh, p->mem, tag, 3, 0 /* attr */, parent);
185 fprintf (outf, "%s ", tag);
187 data_length = atoi_n (buf+entry_p, length_data_entry);
188 entry_p += length_data_entry;
189 data_offset = atoi_n (buf+entry_p, length_starting);
190 entry_p += length_starting;
191 i = data_offset + base_address;
192 end_offset = i+data_length-1;
194 if (data_length <= 0 || data_offset < 0 || end_offset >= record_length)
196 yaz_log(YLOG_WARN, "MARC: Bad offsets in data. Skipping rest");
200 if (memcmp (tag, "00", 2) && indicator_length)
202 /* generate indicator node */
205 const char *attr[10];
212 res = data1_mk_tag(p->dh, p->mem, "datafield", attr, res);
214 for (j = 0; j<indicator_length; j++)
216 char str1[18], str2[2];
217 sprintf (str1, "ind%d", j+1);
224 data1_tag_add_attr (p->dh, p->mem, res, attr);
232 res = data1_mk_tag_n (p->dh, p->mem,
233 buf+i, indicator_length, 0 /* attr */, res);
235 for (j = 0; j<indicator_length; j++)
236 fprintf (outf, "%c", buf[j+i]);
239 i += indicator_length;
245 const char *attr[10];
251 res = data1_mk_tag(p->dh, p->mem, "controlfield", attr, res);
255 /* traverse sub fields */
257 while (buf[i] != ISO2709_RS && buf[i] != ISO2709_FS && i < end_offset)
259 if (memcmp (tag, "00", 2) && identifier_length)
268 for (j = 1; j<identifier_length && j < 9; j++)
269 code[j-1] = buf[i+j];
274 res = data1_mk_tag(p->dh, p->mem, "subfield",
279 res = data1_mk_tag_n (p->dh, p->mem,
280 buf+i+1, identifier_length-1,
281 0 /* attr */, parent);
284 fprintf (outf, " $");
285 for (j = 1; j<identifier_length; j++)
286 fprintf (outf, "%c", buf[j+i]);
289 i += identifier_length;
291 while (buf[i] != ISO2709_RS && buf[i] != ISO2709_IDFS &&
292 buf[i] != ISO2709_FS && i < end_offset)
295 fprintf (outf, "%c", buf[i]);
299 data1_mk_text_n (p->dh, p->mem, buf + i0, i - i0, res);
305 fprintf (outf, "%c", buf[i]);
312 data1_mk_text_n (p->dh, p->mem, buf + i0, i - i0, parent);
315 fprintf (outf, "\n");
317 fprintf (outf, "-- separator but not at end of field\n");
318 if (buf[i] != ISO2709_RS && buf[i] != ISO2709_FS)
319 fprintf (outf, "-- no separator at end of field\n");
326 * Locate some data under this node. This routine should handle variants
329 static char *get_data(data1_node *n, int *len)
335 if (n->which == DATA1N_data)
338 *len = n->u.data.len;
340 for (i = 0; i<*len; i++)
341 if (!d1_isspace(n->u.data.data[i]))
343 while (*len && d1_isspace(n->u.data.data[*len - 1]))
347 return n->u.data.data + i;
349 if (n->which == DATA1N_tag)
351 else if (n->which == DATA1N_data)
361 static data1_node *lookup_subfield(data1_node *node, const char *name)
365 for (p=node; p; p=p->next)
367 if (!yaz_matchstr(p->u.tag.tag, name))
373 static inline_subfield *lookup_inline_subfield(inline_subfield *pisf,
378 for (p=pisf; p; p=p->next)
380 if (!yaz_matchstr(p->name, name))
386 static inline_subfield *cat_inline_subfield(mc_subfield *psf, WRBUF buf,
387 inline_subfield *pisf)
391 for (p = psf; p && pisf; p = p->next)
393 if (p->which == MC_SF)
395 inline_subfield *found = lookup_inline_subfield(pisf, p->name);
399 if (strcmp(p->prefix, "_"))
401 wrbuf_puts(buf, " ");
402 wrbuf_puts(buf, p->prefix);
404 if (p->interval.start == -1)
406 wrbuf_puts(buf, found->data);
410 wrbuf_write(buf, found->data+p->interval.start,
411 p->interval.end-p->interval.start);
414 if (strcmp(p->suffix, "_"))
416 wrbuf_puts(buf, p->suffix);
417 wrbuf_puts(buf, " ");
420 yaz_log(YLOG_LOG, "cat_inline_subfield(): add subfield $%s", found->name);
425 else if (p->which == MC_SFVARIANT)
427 inline_subfield *next;
430 next = cat_inline_subfield(p->u.child, buf, pisf);
436 else if (p->which == MC_SFGROUP)
441 for (pp = p->u.child, found = 0; pp; pp = pp->next)
443 if (!yaz_matchstr(pisf->name, p->name))
451 wrbuf_puts(buf, " (");
452 pisf = cat_inline_subfield(p->u.child, buf, pisf);
453 wrbuf_puts(buf, ") ");
460 static void cat_inline_field(mc_field *pf, WRBUF buf, data1_node *subfield)
462 if (!pf || !subfield)
468 inline_field *pif=NULL;
471 if (yaz_matchstr(subfield->u.tag.tag, "1"))
473 subfield = subfield->next;
478 pif = inline_mk_field();
482 if ((i=inline_parse(pif, psubf->u.tag.tag, get_data(psubf, &len)))<0)
484 yaz_log(YLOG_WARN, "inline subfield ($%s): parse error",
486 inline_destroy_field(pif);
490 } while (psubf && yaz_matchstr(psubf->u.tag.tag, "1"));
494 if (pif && !yaz_matchstr(pif->name, pf->name))
496 if (!pf->list && pif->list)
498 wrbuf_puts(buf, pif->list->data);
508 ind1 = (pif->ind1[0] == ' ') ? '_':pif->ind1[0];
509 ind2 = (pif->ind2[0] == ' ') ? '_':pif->ind2[0];
511 if (((pf->ind1[0] == '.') || (ind1 == pf->ind1[0])) &&
512 ((pf->ind2[0] == '.') || (ind2 == pf->ind2[0])))
514 cat_inline_subfield(pf->list, buf, pif->list);
517 add separator for inline fields
521 wrbuf_puts(buf, "\n");
526 yaz_log(YLOG_WARN, "In-line field %s missed -- indicators do not match", pif->name);
530 inline_destroy_field(pif);
533 yaz_log(YLOG_LOG, "cat_inline_field(): got buffer {%s}", buf);
537 static data1_node *cat_subfield(mc_subfield *psf, WRBUF buf,
538 data1_node *subfield)
542 for (p = psf; p && subfield; p = p->next)
544 if (p->which == MC_SF)
546 data1_node *found = lookup_subfield(subfield, p->name);
552 if (strcmp(p->prefix, "_"))
554 wrbuf_puts(buf, " ");
555 wrbuf_puts(buf, p->prefix);
560 cat_inline_field(p->u.in_line, buf, found);
562 else if (p->interval.start == -1)
564 wrbuf_puts(buf, get_data(found, &len));
568 wrbuf_write(buf, get_data(found, &len)+p->interval.start,
569 p->interval.end-p->interval.start);
572 if (strcmp(p->suffix, "_"))
574 wrbuf_puts(buf, p->suffix);
575 wrbuf_puts(buf, " ");
578 yaz_log(YLOG_LOG, "cat_subfield(): add subfield $%s", found->u.tag.tag);
580 subfield = found->next;
583 else if (p->which == MC_SFVARIANT)
587 next = cat_subfield(p->u.child, buf, subfield);
588 if (next == subfield)
593 else if (p->which == MC_SFGROUP)
598 for (pp = p->u.child, found = 0; pp; pp = pp->next)
600 if (!yaz_matchstr(subfield->u.tag.tag, pp->name))
608 wrbuf_puts(buf, " (");
609 subfield = cat_subfield(p->u.child, buf, subfield);
610 wrbuf_puts(buf, ") ");
617 static data1_node *cat_field(struct grs_read_info *p, mc_field *pf,
618 WRBUF buf, data1_node *field)
620 data1_node *subfield;
627 if (yaz_matchstr(field->u.tag.tag, pf->name))
630 subfield = field->child;
636 check subfield without indicators
639 if (!pf->list && subfield->which == DATA1N_data)
643 if (pf->interval.start == -1)
645 wrbuf_puts(buf, get_data(field, &len));
649 wrbuf_write(buf, get_data(field, &len)+pf->interval.start,
650 pf->interval.end-pf->interval.start);
654 yaz_log(YLOG_LOG, "cat_field(): got buffer {%s}", buf);
663 ind1 = (subfield->u.tag.tag[0] == ' ') ? '_':subfield->u.tag.tag[0];
664 ind2 = (subfield->u.tag.tag[1] == ' ') ? '_':subfield->u.tag.tag[1];
667 ((pf->ind1[0] == '.') || (ind1 == pf->ind1[0])) &&
668 ((pf->ind2[0] == '.') || (ind2 == pf->ind2[0]))
672 yaz_log(YLOG_WARN, "Field %s missed -- does not match indicators", field->u.tag.tag);
677 subfield = subfield->child;
682 cat_subfield(pf->list, buf, subfield);
685 yaz_log(YLOG_LOG, "cat_field(): got buffer {%s}", buf);
691 static int is_empty(char *s)
697 if (!isspace(*(unsigned char *)p))
703 static void parse_data1_tree(struct grs_read_info *p, const char *mc_stmnt,
706 data1_marctab *marctab = data1_absyn_getmarctab(p->dh, root->u.root.absyn);
707 data1_node *top = root->child;
713 c = mc_mk_context(mc_stmnt+3);
722 mc_destroy_context(c);
727 yaz_log(YLOG_LOG, "parse_data1_tree(): statement -{%s}", mc_stmnt);
729 if (!yaz_matchstr(pf->name, "ldr"))
733 yaz_log(YLOG_LOG,"parse_data1_tree(): try LEADER from {%d} to {%d} positions",
734 pf->interval.start, pf->interval.end);
736 new = data1_mk_tag_n(p->dh, p->mem, mc_stmnt, strlen(mc_stmnt), 0, top);
737 data1_mk_text_n(p->dh, p->mem, marctab->leader+pf->interval.start,
738 pf->interval.end-pf->interval.start+1, new);
746 if (!yaz_matchstr(field->u.tag.tag, pf->name))
751 yaz_log(YLOG_LOG, "parse_data1_tree(): try field {%s}", field->u.tag.tag);
756 field = cat_field(p, pf, buf, field);
759 for (pb = strtok(pb, "\n"); pb; pb = strtok(NULL, "\n"))
763 new = data1_mk_tag_n(p->dh, p->mem, mc_stmnt, strlen(mc_stmnt), 0, top);
764 data1_mk_text_n(p->dh, p->mem, pb, strlen(pb), new);
774 mc_destroy_field(pf);
775 mc_destroy_context(c);
779 data1_node *grs_read_marcxml(struct grs_read_info *p)
781 data1_node *root = grs_read_iso2709(p, 1);
787 for (e = data1_absyn_getelements(p->dh, root->u.root.absyn); e; e=e->next)
789 data1_tag *tag = e->tag;
791 if (tag && tag->which == DATA1T_string &&
792 !yaz_matchstr(tag->value.string, "mc?"))
793 parse_data1_tree(p, tag->value.string, root);
798 data1_node *grs_read_marc(struct grs_read_info *p)
800 data1_node *root = grs_read_iso2709(p, 0);
806 for (e = data1_absyn_getelements(p->dh, root->u.root.absyn); e; e=e->next)
808 data1_tag *tag = e->tag;
810 if (tag && tag->which == DATA1T_string &&
811 !yaz_matchstr(tag->value.string, "mc?"))
812 parse_data1_tree(p, tag->value.string, root);
817 static void *init_marc(Res res, RecType rt)
819 struct marc_info *p = xmalloc(sizeof(*p));
824 static ZEBRA_RES config_marc(void *clientData, Res res, const char *args)
826 struct marc_info *p = (struct marc_info*) clientData;
827 if (strlen(args) < sizeof(p->type))
828 strcpy(p->type, args);
832 static void destroy_marc(void *clientData)
834 struct marc_info *p = (struct marc_info*) clientData;
839 static int extract_marc(void *clientData, struct recExtractCtrl *ctrl)
841 return zebra_grs_extract(clientData, ctrl, grs_read_marc);
844 static int retrieve_marc(void *clientData, struct recRetrieveCtrl *ctrl)
846 return zebra_grs_retrieve(clientData, ctrl, grs_read_marc);
849 static struct recType marc_type = {
859 static int extract_marcxml(void *clientData, struct recExtractCtrl *ctrl)
861 return zebra_grs_extract(clientData, ctrl, grs_read_marcxml);
864 static int retrieve_marcxml(void *clientData, struct recRetrieveCtrl *ctrl)
866 return zebra_grs_retrieve(clientData, ctrl, grs_read_marcxml);
869 static struct recType marcxml_type = {
880 #ifdef IDZEBRA_STATIC_GRS_MARC
881 idzebra_filter_grs_marc