1 /* $Id: marcread.c,v 1.9 2007-03-19 21:57:25 adam Exp $
2 Copyright (C) 1995-2007
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 this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
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->stream->readf(p->stream, 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->stream->readf(p->stream, 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);
82 read_bytes = p->stream->readf(p->stream, buf+5, record_length-5);
83 if (read_bytes < record_length-5)
85 yaz_log (YLOG_WARN, "Couldn't read whole MARC record");
88 /* skip until we meet a record separator */
89 while (buf[record_length-1] != ISO2709_RS)
91 if (record_length > sizeof(buf)-2)
93 read_bytes = p->stream->readf(p->stream, buf+record_length, 1);
98 /* read one byte ahead to see if there is more ... */
99 read_bytes = p->stream->readf(p->stream, buf+record_length, 1);
102 off_t cur_offset = p->stream->tellf(p->stream);
105 off_t end_offset = cur_offset - 1;
106 p->stream->endf(p->stream, &end_offset);
110 absynName = mi->type;
111 res_root = data1_mk_root (p->dh, p->mem, absynName);
114 yaz_log (YLOG_WARN, "cannot read MARC without an abstract syntax");
120 const char *attr[] = { "xmlns", "http://www.loc.gov/MARC21/slim", 0};
122 res_top = data1_mk_tag (p->dh, p->mem, "record", attr, res_root);
124 lead = data1_mk_tag(p->dh, p->mem, "leader", 0, res_top);
125 data1_mk_text_n(p->dh, p->mem, buf, 24, lead);
128 res_top = data1_mk_tag (p->dh, p->mem, absynName, 0, res_root);
130 if ((marctab = data1_absyn_getmarctab(p->dh, res_root)))
132 memcpy(marctab->leader, buf, 24);
133 memcpy(marctab->implementation_codes, buf+6, 4);
134 marctab->implementation_codes[4] = '\0';
135 memcpy(marctab->user_systems, buf+17, 3);
136 marctab->user_systems[3] = '\0';
139 if (marctab && marctab->force_indicator_length >= 0)
140 indicator_length = marctab->force_indicator_length;
142 indicator_length = atoi_n (buf+10, 1);
143 if (marctab && marctab->force_identifier_length >= 0)
144 identifier_length = marctab->force_identifier_length;
146 identifier_length = atoi_n (buf+11, 1);
147 base_address = atoi_n (buf+12, 5);
149 length_data_entry = atoi_n (buf+20, 1);
150 length_starting = atoi_n (buf+21, 1);
151 length_implementation = atoi_n (buf+22, 1);
153 for (entry_p = 24; buf[entry_p] != ISO2709_FS; )
155 int l = 3 + length_data_entry + length_starting;
156 if (entry_p + l >= record_length)
158 yaz_log(YLOG_WARN, "MARC: Directory offset %d: end of record.",
162 /* check for digits in length info */
164 if (!isdigit(*(const unsigned char *) (buf + entry_p+l)))
168 /* not all digits, so stop directory scan */
169 yaz_log(YLOG_LOG, "MARC: Bad directory");
172 entry_p += 3 + length_data_entry + length_starting;
174 end_of_directory = entry_p;
175 if (base_address != entry_p+1)
177 yaz_log(YLOG_WARN, "MARC: Base address does not follow directory");
179 for (entry_p = 24; entry_p != end_of_directory; )
187 data1_node *parent = res_top;
189 memcpy (tag, buf+entry_p, 3);
196 res = data1_mk_tag_n (p->dh, p->mem, tag, 3, 0 /* attr */, parent);
199 fprintf (outf, "%s ", tag);
201 data_length = atoi_n (buf+entry_p, length_data_entry);
202 entry_p += length_data_entry;
203 data_offset = atoi_n (buf+entry_p, length_starting);
204 entry_p += length_starting;
205 i = data_offset + base_address;
206 end_offset = i+data_length-1;
208 if (data_length <= 0 || data_offset < 0 || end_offset >= record_length)
210 yaz_log(YLOG_WARN, "MARC: Bad offsets in data. Skipping rest");
214 if (memcmp (tag, "00", 2) && indicator_length)
216 /* generate indicator node */
219 const char *attr[10];
226 res = data1_mk_tag(p->dh, p->mem, "datafield", attr, res);
228 for (j = 0; j<indicator_length; j++)
230 char str1[18], str2[2];
231 sprintf (str1, "ind%d", j+1);
238 data1_tag_add_attr (p->dh, p->mem, res, attr);
246 res = data1_mk_tag_n (p->dh, p->mem,
247 buf+i, indicator_length, 0 /* attr */, res);
249 for (j = 0; j<indicator_length; j++)
250 fprintf (outf, "%c", buf[j+i]);
253 i += indicator_length;
259 const char *attr[10];
265 res = data1_mk_tag(p->dh, p->mem, "controlfield", attr, res);
269 /* traverse sub fields */
271 while (buf[i] != ISO2709_RS && buf[i] != ISO2709_FS && i < end_offset)
273 if (memcmp (tag, "00", 2) && identifier_length)
282 for (j = 1; j<identifier_length && j < 9; j++)
283 code[j-1] = buf[i+j];
288 res = data1_mk_tag(p->dh, p->mem, "subfield",
293 res = data1_mk_tag_n (p->dh, p->mem,
294 buf+i+1, identifier_length-1,
295 0 /* attr */, parent);
298 fprintf (outf, " $");
299 for (j = 1; j<identifier_length; j++)
300 fprintf (outf, "%c", buf[j+i]);
303 i += identifier_length;
305 while (buf[i] != ISO2709_RS && buf[i] != ISO2709_IDFS &&
306 buf[i] != ISO2709_FS && i < end_offset)
309 fprintf (outf, "%c", buf[i]);
313 data1_mk_text_n (p->dh, p->mem, buf + i0, i - i0, res);
319 fprintf (outf, "%c", buf[i]);
326 data1_mk_text_n (p->dh, p->mem, buf + i0, i - i0, parent);
329 fprintf (outf, "\n");
331 fprintf (outf, "-- separator but not at end of field\n");
332 if (buf[i] != ISO2709_RS && buf[i] != ISO2709_FS)
333 fprintf (outf, "-- no separator at end of field\n");
340 * Locate some data under this node. This routine should handle variants
343 static char *get_data(data1_node *n, int *len)
349 if (n->which == DATA1N_data)
352 *len = n->u.data.len;
354 for (i = 0; i<*len; i++)
355 if (!d1_isspace(n->u.data.data[i]))
357 while (*len && d1_isspace(n->u.data.data[*len - 1]))
361 return n->u.data.data + i;
363 if (n->which == DATA1N_tag)
365 else if (n->which == DATA1N_data)
375 static data1_node *lookup_subfield(data1_node *node, const char *name)
379 for (p=node; p; p=p->next)
381 if (!yaz_matchstr(p->u.tag.tag, name))
387 static inline_subfield *lookup_inline_subfield(inline_subfield *pisf,
392 for (p=pisf; p; p=p->next)
394 if (!yaz_matchstr(p->name, name))
400 static inline_subfield *cat_inline_subfield(mc_subfield *psf, WRBUF buf,
401 inline_subfield *pisf)
405 for (p = psf; p && pisf; p = p->next)
407 if (p->which == MC_SF)
409 inline_subfield *found = lookup_inline_subfield(pisf, p->name);
413 if (strcmp(p->prefix, "_"))
415 wrbuf_puts(buf, " ");
416 wrbuf_puts(buf, p->prefix);
418 if (p->interval.start == -1)
420 wrbuf_puts(buf, found->data);
424 wrbuf_write(buf, found->data+p->interval.start,
425 p->interval.end-p->interval.start);
428 if (strcmp(p->suffix, "_"))
430 wrbuf_puts(buf, p->suffix);
431 wrbuf_puts(buf, " ");
434 yaz_log(YLOG_LOG, "cat_inline_subfield(): add subfield $%s", found->name);
439 else if (p->which == MC_SFVARIANT)
441 inline_subfield *next;
444 next = cat_inline_subfield(p->u.child, buf, pisf);
450 else if (p->which == MC_SFGROUP)
455 for (pp = p->u.child, found = 0; pp; pp = pp->next)
457 if (!yaz_matchstr(pisf->name, p->name))
465 wrbuf_puts(buf, " (");
466 pisf = cat_inline_subfield(p->u.child, buf, pisf);
467 wrbuf_puts(buf, ") ");
474 static void cat_inline_field(mc_field *pf, WRBUF buf, data1_node *subfield)
476 if (!pf || !subfield)
482 inline_field *pif=NULL;
485 if (yaz_matchstr(subfield->u.tag.tag, "1"))
487 subfield = subfield->next;
492 pif = inline_mk_field();
496 if ((i=inline_parse(pif, psubf->u.tag.tag, get_data(psubf, &len)))<0)
498 yaz_log(YLOG_WARN, "inline subfield ($%s): parse error",
500 inline_destroy_field(pif);
504 } while (psubf && yaz_matchstr(psubf->u.tag.tag, "1"));
508 if (pif && !yaz_matchstr(pif->name, pf->name))
510 if (!pf->list && pif->list)
512 wrbuf_puts(buf, pif->list->data);
522 ind1 = (pif->ind1[0] == ' ') ? '_':pif->ind1[0];
523 ind2 = (pif->ind2[0] == ' ') ? '_':pif->ind2[0];
525 if (((pf->ind1[0] == '.') || (ind1 == pf->ind1[0])) &&
526 ((pf->ind2[0] == '.') || (ind2 == pf->ind2[0])))
528 cat_inline_subfield(pf->list, buf, pif->list);
531 add separator for inline fields
535 wrbuf_puts(buf, "\n");
540 yaz_log(YLOG_WARN, "In-line field %s missed -- indicators do not match", pif->name);
544 inline_destroy_field(pif);
547 yaz_log(YLOG_LOG, "cat_inline_field(): got buffer {%s}", buf);
551 static data1_node *cat_subfield(mc_subfield *psf, WRBUF buf,
552 data1_node *subfield)
556 for (p = psf; p && subfield; p = p->next)
558 if (p->which == MC_SF)
560 data1_node *found = lookup_subfield(subfield, p->name);
566 if (strcmp(p->prefix, "_"))
568 wrbuf_puts(buf, " ");
569 wrbuf_puts(buf, p->prefix);
574 cat_inline_field(p->u.in_line, buf, found);
576 else if (p->interval.start == -1)
578 wrbuf_puts(buf, get_data(found, &len));
582 wrbuf_write(buf, get_data(found, &len)+p->interval.start,
583 p->interval.end-p->interval.start);
586 if (strcmp(p->suffix, "_"))
588 wrbuf_puts(buf, p->suffix);
589 wrbuf_puts(buf, " ");
592 yaz_log(YLOG_LOG, "cat_subfield(): add subfield $%s", found->u.tag.tag);
594 subfield = found->next;
597 else if (p->which == MC_SFVARIANT)
601 next = cat_subfield(p->u.child, buf, subfield);
602 if (next == subfield)
607 else if (p->which == MC_SFGROUP)
612 for (pp = p->u.child, found = 0; pp; pp = pp->next)
614 if (!yaz_matchstr(subfield->u.tag.tag, pp->name))
622 wrbuf_puts(buf, " (");
623 subfield = cat_subfield(p->u.child, buf, subfield);
624 wrbuf_puts(buf, ") ");
631 static data1_node *cat_field(struct grs_read_info *p, mc_field *pf,
632 WRBUF buf, data1_node *field)
634 data1_node *subfield;
641 if (yaz_matchstr(field->u.tag.tag, pf->name))
644 subfield = field->child;
650 check subfield without indicators
653 if (!pf->list && subfield->which == DATA1N_data)
657 if (pf->interval.start == -1)
659 wrbuf_puts(buf, get_data(field, &len));
663 wrbuf_write(buf, get_data(field, &len)+pf->interval.start,
664 pf->interval.end-pf->interval.start);
668 yaz_log(YLOG_LOG, "cat_field(): got buffer {%s}", buf);
677 ind1 = (subfield->u.tag.tag[0] == ' ') ? '_':subfield->u.tag.tag[0];
678 ind2 = (subfield->u.tag.tag[1] == ' ') ? '_':subfield->u.tag.tag[1];
681 ((pf->ind1[0] == '.') || (ind1 == pf->ind1[0])) &&
682 ((pf->ind2[0] == '.') || (ind2 == pf->ind2[0]))
686 yaz_log(YLOG_WARN, "Field %s missed -- does not match indicators", field->u.tag.tag);
691 subfield = subfield->child;
696 cat_subfield(pf->list, buf, subfield);
699 yaz_log(YLOG_LOG, "cat_field(): got buffer {%s}", buf);
705 static int is_empty(char *s)
711 if (!isspace(*(unsigned char *)p))
717 static void parse_data1_tree(struct grs_read_info *p, const char *mc_stmnt,
720 data1_marctab *marctab = data1_absyn_getmarctab(p->dh, root);
721 data1_node *top = root->child;
727 c = mc_mk_context(mc_stmnt+3);
736 mc_destroy_context(c);
741 yaz_log(YLOG_LOG, "parse_data1_tree(): statement -{%s}", mc_stmnt);
743 if (!yaz_matchstr(pf->name, "ldr"))
747 yaz_log(YLOG_LOG,"parse_data1_tree(): try LEADER from {%d} to {%d} positions",
748 pf->interval.start, pf->interval.end);
752 new = data1_mk_tag_n(p->dh, p->mem, mc_stmnt, strlen(mc_stmnt), 0, top);
753 data1_mk_text_n(p->dh, p->mem, marctab->leader+pf->interval.start,
754 pf->interval.end-pf->interval.start+1, new);
763 if (!yaz_matchstr(field->u.tag.tag, pf->name))
768 yaz_log(YLOG_LOG, "parse_data1_tree(): try field {%s}", field->u.tag.tag);
773 field = cat_field(p, pf, buf, field);
777 for (pb = strtok(pb, "\n"); pb; pb = strtok(NULL, "\n"))
781 new = data1_mk_tag_n(p->dh, p->mem, mc_stmnt, strlen(mc_stmnt), 0, top);
782 data1_mk_text_n(p->dh, p->mem, pb, strlen(pb), new);
792 mc_destroy_field(pf);
793 mc_destroy_context(c);
797 data1_node *grs_read_marcxml(struct grs_read_info *p)
799 data1_node *root = grs_read_iso2709(p, 1);
805 for (e = data1_absyn_getelements(p->dh, root); e; e=e->next)
807 data1_tag *tag = e->tag;
809 if (tag && tag->which == DATA1T_string &&
810 !yaz_matchstr(tag->value.string, "mc?"))
811 parse_data1_tree(p, tag->value.string, root);
816 data1_node *grs_read_marc(struct grs_read_info *p)
818 data1_node *root = grs_read_iso2709(p, 0);
824 for (e = data1_absyn_getelements(p->dh, root); e; e=e->next)
826 data1_tag *tag = e->tag;
828 if (tag && tag->which == DATA1T_string &&
829 !yaz_matchstr(tag->value.string, "mc?"))
830 parse_data1_tree(p, tag->value.string, root);
835 static void *init_marc(Res res, RecType rt)
837 struct marc_info *p = xmalloc(sizeof(*p));
842 static ZEBRA_RES config_marc(void *clientData, Res res, const char *args)
844 struct marc_info *p = (struct marc_info*) clientData;
845 if (strlen(args) < sizeof(p->type))
846 strcpy(p->type, args);
850 static void destroy_marc(void *clientData)
852 struct marc_info *p = (struct marc_info*) clientData;
857 static int extract_marc(void *clientData, struct recExtractCtrl *ctrl)
859 return zebra_grs_extract(clientData, ctrl, grs_read_marc);
862 static int retrieve_marc(void *clientData, struct recRetrieveCtrl *ctrl)
864 return zebra_grs_retrieve(clientData, ctrl, grs_read_marc);
867 static struct recType marc_type = {
877 static int extract_marcxml(void *clientData, struct recExtractCtrl *ctrl)
879 return zebra_grs_extract(clientData, ctrl, grs_read_marcxml);
882 static int retrieve_marcxml(void *clientData, struct recRetrieveCtrl *ctrl)
884 return zebra_grs_retrieve(clientData, ctrl, grs_read_marcxml);
887 static struct recType marcxml_type = {
898 #ifdef IDZEBRA_STATIC_GRS_MARC
899 idzebra_filter_grs_marc
913 * indent-tabs-mode: nil
915 * vim: shiftwidth=4 tabstop=8 expandtab