Happy new year
[idzebra-moved-to-github.git] / index / mod_grs_marc.c
1 /* This file is part of the Zebra server.
2    Copyright (C) 1994-2009 Index Data
3
4 Zebra is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
7 version.
8
9 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17
18 */
19
20 #include <stdio.h>
21 #include <ctype.h>
22 #include <assert.h>
23
24 #include <yaz/yaz-util.h>
25 #include <yaz/marcdisp.h>
26 #include <idzebra/recgrs.h>
27 #include "marcomp.h"
28 #include "inline.h"
29
30 #define MARC_DEBUG 0
31 #define MARCOMP_DEBUG 0
32
33 struct marc_info {
34     char type[256];
35 };
36
37 static data1_node *grs_read_iso2709 (struct grs_read_info *p, int marc_xml)
38 {
39     struct marc_info *mi = (struct marc_info*) p->clientData;
40     char buf[100000];
41     int entry_p;
42     int record_length;
43     int indicator_length;
44     int identifier_length;
45     int base_address;
46     int end_of_directory;
47     int length_data_entry;
48     int length_starting;
49     int length_implementation;
50     int read_bytes;
51 #if MARC_DEBUG
52     FILE *outf = stdout;
53 #endif
54     data1_node *res_root, *res_top;
55     char *absynName;
56     data1_marctab *marctab;
57
58     if (p->stream->readf(p->stream, buf, 5) != 5)
59         return NULL;
60     while (*buf < '0' || *buf > '9')
61     {
62         int i;
63
64         yaz_log(YLOG_WARN, "MARC: Skipping bad byte %d (0x%02X)",
65                 *buf & 0xff, *buf & 0xff);
66         for (i = 0; i<4; i++)
67             buf[i] = buf[i+1];
68
69         if (p->stream->readf(p->stream, buf+4, 1) != 1)
70             return NULL;
71     }
72     record_length = atoi_n (buf, 5);
73     if (record_length < 25)
74     {
75         yaz_log (YLOG_WARN, "MARC record length < 25, is %d", record_length);
76         return NULL;
77     }
78
79     read_bytes = p->stream->readf(p->stream, buf+5, record_length-5);
80     if (read_bytes < record_length-5)
81     {
82         yaz_log (YLOG_WARN, "Couldn't read whole MARC record");
83         return NULL;
84     }
85     /* skip until we meet a record separator */
86     while (buf[record_length-1] != ISO2709_RS)
87     {
88         if (record_length > sizeof(buf)-2)
89             break;
90         read_bytes = p->stream->readf(p->stream, buf+record_length, 1);
91         if (read_bytes != 1)
92             break;
93         record_length++;
94     }
95     /* read one byte ahead to see if there is more ... */
96     read_bytes = p->stream->readf(p->stream, buf+record_length, 1);
97     if (read_bytes == 1)
98     {
99         off_t cur_offset = p->stream->tellf(p->stream);
100         if (p->stream->endf)
101         {
102             off_t end_offset = cur_offset - 1;
103             p->stream->endf(p->stream, &end_offset);
104         }
105     }
106
107     absynName = mi->type;
108     res_root = data1_mk_root (p->dh, p->mem, absynName);
109     if (!res_root)
110     {
111         yaz_log (YLOG_WARN, "cannot read MARC without an abstract syntax");
112         return 0;
113     }
114     if (marc_xml)
115     {
116         data1_node *lead;
117         const char *attr[] = { "xmlns", "http://www.loc.gov/MARC21/slim", 0};
118                          
119         res_top = data1_mk_tag (p->dh, p->mem, "record", attr, res_root);
120
121         lead = data1_mk_tag(p->dh, p->mem, "leader", 0, res_top);
122         data1_mk_text_n(p->dh, p->mem, buf, 24, lead);
123     }
124     else
125         res_top = data1_mk_tag (p->dh, p->mem, absynName, 0, res_root);
126
127     if ((marctab = data1_absyn_getmarctab(p->dh, res_root)))
128     {
129         memcpy(marctab->leader, buf, 24);
130         memcpy(marctab->implementation_codes, buf+6, 4);
131         marctab->implementation_codes[4] = '\0';
132         memcpy(marctab->user_systems, buf+17, 3);
133         marctab->user_systems[3] = '\0';
134     }
135
136     if (marctab && marctab->force_indicator_length >= 0)
137         indicator_length = marctab->force_indicator_length;
138     else
139         indicator_length = atoi_n (buf+10, 1);
140     if (marctab && marctab->force_identifier_length >= 0)
141         identifier_length = marctab->force_identifier_length;
142     else
143         identifier_length = atoi_n (buf+11, 1);
144     base_address = atoi_n (buf+12, 5);
145
146     length_data_entry = atoi_n (buf+20, 1);
147     length_starting = atoi_n (buf+21, 1);
148     length_implementation = atoi_n (buf+22, 1);
149
150     for (entry_p = 24; buf[entry_p] != ISO2709_FS; )
151     {
152         int l = 3 + length_data_entry + length_starting;
153         if (entry_p + l >= record_length)
154         {
155             yaz_log(YLOG_WARN, "MARC: Directory offset %d: end of record.",
156                     entry_p);
157             return 0;
158         }
159         /* check for digits in length info */
160         while (--l >= 3)
161             if (!isdigit(*(const unsigned char *) (buf + entry_p+l)))
162                 break;
163         if (l >= 3)
164         {
165             /* not all digits, so stop directory scan */
166             yaz_log(YLOG_LOG, "MARC: Bad directory");
167             break;
168         }
169         entry_p += 3 + length_data_entry + length_starting;
170     }
171     end_of_directory = entry_p;
172     if (base_address != entry_p+1)
173     {
174         yaz_log(YLOG_WARN, "MARC: Base address does not follow directory");
175     }
176     for (entry_p = 24; entry_p != end_of_directory; )
177     {
178         int data_length;
179         int data_offset;
180         int end_offset;
181         int i, i0;
182         char tag[4];
183         data1_node *res;
184         data1_node *parent = res_top;
185
186         memcpy (tag, buf+entry_p, 3);
187         entry_p += 3;
188         tag[3] = '\0';
189
190         if (marc_xml)
191             res = parent;
192         else
193             res = data1_mk_tag_n (p->dh, p->mem, tag, 3, 0 /* attr */, parent);
194         
195 #if MARC_DEBUG
196         fprintf (outf, "%s ", tag);
197 #endif
198         data_length = atoi_n (buf+entry_p, length_data_entry);
199         entry_p += length_data_entry;
200         data_offset = atoi_n (buf+entry_p, length_starting);
201         entry_p += length_starting;
202         i = data_offset + base_address;
203         end_offset = i+data_length-1;
204
205         if (data_length <= 0 || data_offset < 0 || end_offset >= record_length)
206         {
207             yaz_log(YLOG_WARN, "MARC: Bad offsets in data. Skipping rest");
208             break;
209         }
210         
211         if (memcmp (tag, "00", 2) && indicator_length)
212         {
213             /* generate indicator node */
214             if (marc_xml)
215             {
216                 const char *attr[10];
217                 int j;
218
219                 attr[0] = "tag";
220                 attr[1] = tag;
221                 attr[2] = 0;
222
223                 res = data1_mk_tag(p->dh, p->mem, "datafield", attr, res);
224
225                 for (j = 0; j<indicator_length; j++)
226                 {
227                     char str1[18], str2[2];
228                     sprintf (str1, "ind%d", j+1);
229                     str2[0] = buf[i+j];
230                     str2[1] = '\0';
231
232                     attr[0] = str1;
233                     attr[1] = str2;
234                     
235                     data1_tag_add_attr (p->dh, p->mem, res, attr);
236                 }
237             }
238             else
239             {
240 #if MARC_DEBUG
241                 int j;
242 #endif
243                 res = data1_mk_tag_n (p->dh, p->mem, 
244                                       buf+i, indicator_length, 0 /* attr */, res);
245 #if MARC_DEBUG
246                 for (j = 0; j<indicator_length; j++)
247                     fprintf (outf, "%c", buf[j+i]);
248 #endif
249             }
250             i += indicator_length;
251         } 
252         else
253         {
254             if (marc_xml)
255             {
256                 const char *attr[10];
257                 
258                 attr[0] = "tag";
259                 attr[1] = tag;
260                 attr[2] = 0;
261                 
262                 res = data1_mk_tag(p->dh, p->mem, "controlfield", attr, res);
263             }
264         }
265         parent = res;
266         /* traverse sub fields */
267         i0 = i;
268         while (buf[i] != ISO2709_RS && buf[i] != ISO2709_FS && i < end_offset)
269         {
270             if (memcmp (tag, "00", 2) && identifier_length)
271             {
272                 data1_node *res;
273                 if (marc_xml)
274                 {
275                     int j;
276                     const char *attr[3];
277                     char code[10];
278                     
279                     for (j = 1; j<identifier_length && j < 9; j++)
280                         code[j-1] = buf[i+j];
281                     code[j-1] = 0;
282                     attr[0] = "code";
283                     attr[1] = code;
284                     attr[2] = 0;
285                     res = data1_mk_tag(p->dh, p->mem, "subfield",
286                                        attr, parent);
287                 }
288                 else
289                 {
290                     res = data1_mk_tag_n (p->dh, p->mem,
291                                            buf+i+1, identifier_length-1, 
292                                            0 /* attr */, parent);
293                 }
294 #if MARC_DEBUG
295                 fprintf (outf, " $"); 
296                 for (j = 1; j<identifier_length; j++)
297                     fprintf (outf, "%c", buf[j+i]);
298                 fprintf (outf, " ");
299 #endif
300                 i += identifier_length;
301                 i0 = i;
302                 while (buf[i] != ISO2709_RS && buf[i] != ISO2709_IDFS &&
303                      buf[i] != ISO2709_FS && i < end_offset)
304                 {
305 #if MARC_DEBUG
306                     fprintf (outf, "%c", buf[i]);
307 #endif
308                     i++;
309                 }
310                 data1_mk_text_n (p->dh, p->mem, buf + i0, i - i0, res);
311                 i0 = i;
312             }
313             else
314             {
315 #if MARC_DEBUG
316                 fprintf (outf, "%c", buf[i]);
317 #endif
318                 i++;
319             }
320         }
321         if (i > i0)
322         {
323             data1_mk_text_n (p->dh, p->mem, buf + i0, i - i0, parent);
324         }
325 #if MARC_DEBUG
326         fprintf (outf, "\n");
327         if (i < end_offset)
328             fprintf (outf, "-- separator but not at end of field\n");
329         if (buf[i] != ISO2709_RS && buf[i] != ISO2709_FS)
330             fprintf (outf, "-- no separator at end of field\n");
331 #endif
332     }
333     return res_root;
334 }
335
336 /*
337  * Locate some data under this node. This routine should handle variants
338  * prettily.
339  */
340 static char *get_data(data1_node *n, int *len)
341 {
342     char *r;
343
344     while (n)
345     {
346         if (n->which == DATA1N_data)
347         {
348             int i;
349             *len = n->u.data.len;
350
351             for (i = 0; i<*len; i++)
352                 if (!d1_isspace(n->u.data.data[i]))
353                     break;
354             while (*len && d1_isspace(n->u.data.data[*len - 1]))
355                 (*len)--;
356             *len = *len - i;
357             if (*len > 0)
358                 return n->u.data.data + i;
359         }
360         if (n->which == DATA1N_tag)
361             n = n->child;
362         else if (n->which == DATA1N_data)
363             n = n->next;
364         else
365             break;      
366     }
367     r = "";
368     *len = strlen(r);
369     return r;
370 }
371
372 static data1_node *lookup_subfield(data1_node *node, const char *name)
373 {
374     data1_node *p;
375     
376     for (p=node; p; p=p->next)
377     {
378         if (!yaz_matchstr(p->u.tag.tag, name))
379             return p;
380     }
381     return 0;
382 }
383
384 static inline_subfield *lookup_inline_subfield(inline_subfield *pisf,
385                                                const char *name)
386 {
387     inline_subfield *p;
388     
389     for (p=pisf; p; p=p->next)
390     {
391         if (!yaz_matchstr(p->name, name))
392             return p;
393     }
394     return 0;
395 }
396
397 static inline_subfield *cat_inline_subfield(mc_subfield *psf, WRBUF buf,
398                                             inline_subfield *pisf)
399 {
400     mc_subfield *p;
401     
402     for (p = psf; p && pisf; p = p->next)
403     {
404         if (p->which == MC_SF)
405         {
406             inline_subfield *found = lookup_inline_subfield(pisf, p->name);
407             
408             if (found)
409             {
410                 if (strcmp(p->prefix, "_"))
411                 {
412                     wrbuf_puts(buf, " ");
413                     wrbuf_puts(buf, p->prefix);
414                 }
415                 if (p->interval.start == -1)
416                 {
417                     wrbuf_puts(buf, found->data);
418                 }
419                 else
420                 {
421                     wrbuf_write(buf, found->data+p->interval.start,
422                                 p->interval.end-p->interval.start);
423                     wrbuf_puts(buf, "");
424                 }
425                 if (strcmp(p->suffix, "_"))
426                 {
427                     wrbuf_puts(buf, p->suffix);
428                     wrbuf_puts(buf, " ");
429                 }
430 #if MARCOMP_DEBUG
431                 yaz_log(YLOG_LOG, "cat_inline_subfield(): add subfield $%s", found->name);
432 #endif          
433                 pisf = found->next;
434             }
435         }
436         else if (p->which == MC_SFVARIANT)
437         {
438             inline_subfield *next;
439             
440             do {
441                 next = cat_inline_subfield(p->u.child, buf, pisf);
442                 if (next == pisf)
443                     break;
444                 pisf = next;
445             } while (pisf);
446         }
447         else if (p->which == MC_SFGROUP)
448         {
449             mc_subfield *pp;
450             int found;
451             
452             for (pp = p->u.child, found = 0; pp; pp = pp->next)
453             {
454                 if (!yaz_matchstr(pisf->name, p->name))
455                 {
456                     found = 1;
457                     break;
458                 }
459             }
460             if (found)
461             {
462                 wrbuf_puts(buf, " (");
463                 pisf = cat_inline_subfield(p->u.child, buf, pisf);
464                 wrbuf_puts(buf, ") ");
465             }
466         }
467     }
468     return pisf; 
469 }
470
471 static void cat_inline_field(mc_field *pf, WRBUF buf, data1_node *subfield)
472 {    
473     if (!pf || !subfield)
474         return;
475
476     for (;subfield;)
477     {
478         int len;
479         inline_field *pif=NULL;
480         data1_node *psubf;
481         
482         if (yaz_matchstr(subfield->u.tag.tag, "1"))
483         {
484             subfield = subfield->next;
485             continue;
486         }
487         
488         psubf = subfield;
489         pif = inline_mk_field();
490         do
491         {
492             int i;
493             if ((i=inline_parse(pif, psubf->u.tag.tag, get_data(psubf, &len)))<0)
494             {
495                 yaz_log(YLOG_WARN, "inline subfield ($%s): parse error",
496                     psubf->u.tag.tag);
497                 inline_destroy_field(pif);
498                 return; 
499             }
500             psubf = psubf->next;
501         } while (psubf && yaz_matchstr(psubf->u.tag.tag, "1"));
502         
503         subfield = psubf;
504         
505         if (pif && !yaz_matchstr(pif->name, pf->name))
506         {
507             if (!pf->list && pif->list)
508             {
509                 wrbuf_puts(buf, pif->list->data);
510             }
511             else
512             {
513                 int ind1, ind2;
514
515                 /*
516                     check indicators
517                 */
518
519                 ind1 = (pif->ind1[0] == ' ') ? '_':pif->ind1[0];
520                 ind2 = (pif->ind2[0] == ' ') ? '_':pif->ind2[0];
521     
522                 if (((pf->ind1[0] == '.') || (ind1 == pf->ind1[0])) &&
523                     ((pf->ind2[0] == '.') || (ind2 == pf->ind2[0])))
524                 {
525                     cat_inline_subfield(pf->list, buf, pif->list);
526                     
527                     /*
528                         add separator for inline fields
529                     */
530                     if (wrbuf_len(buf))
531                     {
532                         wrbuf_puts(buf, "\n");
533                     }
534                 }
535                 else
536                 {
537                     yaz_log(YLOG_WARN, "In-line field %s missed -- indicators do not match", pif->name);
538                 }
539             }
540         }
541         inline_destroy_field(pif);
542     }
543 #if MARCOMP_DEBUG    
544     yaz_log(YLOG_LOG, "cat_inline_field(): got buffer {%s}", buf);
545 #endif
546 }
547
548 static data1_node *cat_subfield(mc_subfield *psf, WRBUF buf,
549                                 data1_node *subfield)
550 {
551     mc_subfield *p;
552     
553     for (p = psf; p && subfield; p = p->next)
554     {
555         if (p->which == MC_SF)
556         {
557             data1_node *found = lookup_subfield(subfield, p->name);
558             
559             if (found)
560             {
561                 int len;
562                 
563                 if (strcmp(p->prefix, "_"))
564                 {
565                     wrbuf_puts(buf, " ");
566                     wrbuf_puts(buf, p->prefix);
567                 }
568                 
569                 if (p->u.in_line)
570                 {
571                     cat_inline_field(p->u.in_line, buf, found);
572                 }
573                 else if (p->interval.start == -1)
574                 {
575                     wrbuf_puts(buf, get_data(found, &len));
576                 }
577                 else
578                 {
579                     wrbuf_write(buf, get_data(found, &len)+p->interval.start,
580                         p->interval.end-p->interval.start);
581                     wrbuf_puts(buf, "");
582                 }
583                 if (strcmp(p->suffix, "_"))
584                 {
585                     wrbuf_puts(buf, p->suffix);
586                     wrbuf_puts(buf, " ");
587                 }
588 #if MARCOMP_DEBUG               
589                 yaz_log(YLOG_LOG, "cat_subfield(): add subfield $%s", found->u.tag.tag);
590 #endif          
591                 subfield = found->next;
592             }
593         }
594         else if (p->which == MC_SFVARIANT)
595         {
596             data1_node *next;
597             do {
598                 next = cat_subfield(p->u.child, buf, subfield);
599                 if (next == subfield)
600                     break;
601                 subfield = next;
602             } while (subfield);
603         }
604         else if (p->which == MC_SFGROUP)
605         {
606             mc_subfield *pp;
607             int found;
608             
609             for (pp = p->u.child, found = 0; pp; pp = pp->next)
610             {
611                 if (!yaz_matchstr(subfield->u.tag.tag, pp->name))
612                 {
613                     found = 1;
614                     break;
615                 }
616             }
617             if (found)
618             {
619                 wrbuf_puts(buf, " (");
620                 subfield = cat_subfield(p->u.child, buf, subfield);
621                 wrbuf_puts(buf, ") ");
622             }
623         }
624     }
625     return subfield;
626 }
627
628 static data1_node *cat_field(struct grs_read_info *p, mc_field *pf,
629                              WRBUF buf, data1_node *field)
630 {
631     data1_node *subfield;
632     int ind1, ind2;
633     
634     if (!pf || !field)
635         return 0;
636
637     
638     if (yaz_matchstr(field->u.tag.tag, pf->name))
639         return field->next;
640
641     subfield = field->child;
642     
643     if (!subfield)
644         return field->next;
645
646     /*
647         check subfield without indicators
648     */
649     
650     if (!pf->list && subfield->which == DATA1N_data)
651     {
652         int len;
653         
654         if (pf->interval.start == -1)
655         {
656             wrbuf_puts(buf, get_data(field, &len));
657         }
658         else
659         {
660             wrbuf_write(buf, get_data(field, &len)+pf->interval.start,
661                         pf->interval.end-pf->interval.start);
662             wrbuf_puts(buf, "");
663         }
664 #if MARCOMP_DEBUG
665         yaz_log(YLOG_LOG, "cat_field(): got buffer {%s}", buf);
666 #endif
667         return field->next;
668     }
669     
670     /*
671         check indicators
672     */
673
674     ind1 = (subfield->u.tag.tag[0] == ' ') ? '_':subfield->u.tag.tag[0];
675     ind2 = (subfield->u.tag.tag[1] == ' ') ? '_':subfield->u.tag.tag[1];
676     
677     if (!(
678         ((pf->ind1[0] == '.') || (ind1 == pf->ind1[0])) &&
679         ((pf->ind2[0] == '.') || (ind2 == pf->ind2[0]))
680         ))
681     {
682 #if MARCOMP_DEBUG
683         yaz_log(YLOG_WARN, "Field %s missed -- does not match indicators", field->u.tag.tag);
684 #endif
685         return field->next;
686     }
687     
688     subfield = subfield->child;
689     
690     if (!subfield)
691         return field->next;
692
693     cat_subfield(pf->list, buf, subfield);
694
695 #if MARCOMP_DEBUG    
696     yaz_log(YLOG_LOG, "cat_field(): got buffer {%s}", buf);
697 #endif
698     
699     return field->next;    
700 }
701
702 static int is_empty(char *s)
703 {
704     char *p = s;
705     
706     for (p = s; *p; p++)
707     {
708         if (!isspace(*(unsigned char *)p))
709             return 0;
710     }
711     return 1;
712 }
713
714 static void parse_data1_tree(struct grs_read_info *p, const char *mc_stmnt,
715                              data1_node *root)
716 {
717     data1_marctab *marctab = data1_absyn_getmarctab(p->dh, root);
718     data1_node *top = root->child;
719     data1_node *field;
720     mc_context *c;
721     mc_field *pf;
722     WRBUF buf;
723     
724     c = mc_mk_context(mc_stmnt+3);
725     
726     if (!c)
727         return;
728         
729     pf = mc_getfield(c);
730     
731     if (!pf)
732     {
733         mc_destroy_context(c);
734         return;
735     }
736     buf = wrbuf_alloc();
737 #if MARCOMP_DEBUG    
738     yaz_log(YLOG_LOG, "parse_data1_tree(): statement -{%s}", mc_stmnt);
739 #endif
740     if (!yaz_matchstr(pf->name, "ldr"))
741     {
742         data1_node *new;
743 #if MARCOMP_DEBUG
744         yaz_log(YLOG_LOG,"parse_data1_tree(): try LEADER from {%d} to {%d} positions",
745             pf->interval.start, pf->interval.end);
746 #endif  
747         if (marctab)
748         {
749             new = data1_mk_tag_n(p->dh, p->mem, mc_stmnt, strlen(mc_stmnt), 0, top);
750             data1_mk_text_n(p->dh, p->mem, marctab->leader+pf->interval.start,
751                             pf->interval.end-pf->interval.start+1, new);
752         }
753     }
754     else
755     {
756         field=top->child;
757         
758         while(field)
759         {
760             if (!yaz_matchstr(field->u.tag.tag, pf->name))
761             {
762                 data1_node *new;
763                 char *pb;
764 #if MARCOMP_DEBUG               
765                 yaz_log(YLOG_LOG, "parse_data1_tree(): try field {%s}", field->u.tag.tag);
766 #endif          
767                 wrbuf_rewind(buf);
768                 wrbuf_puts(buf, "");
769
770                 field = cat_field(p, pf, buf, field);
771                 
772                 wrbuf_cstr(buf);
773                 pb = wrbuf_buf(buf);
774                 for (pb = strtok(pb, "\n"); pb; pb = strtok(NULL, "\n"))
775                 {
776                     if (!is_empty(pb))
777                     {
778                         new = data1_mk_tag_n(p->dh, p->mem, mc_stmnt, strlen(mc_stmnt), 0, top);
779                         data1_mk_text_n(p->dh, p->mem, pb, strlen(pb), new);
780                     }
781                 }
782             }
783             else
784             {
785                 field = field->next;
786             }
787         }
788     }
789     mc_destroy_field(pf);
790     mc_destroy_context(c);
791     wrbuf_destroy(buf);
792 }
793
794 data1_node *grs_read_marcxml(struct grs_read_info *p)
795 {
796     data1_node *root = grs_read_iso2709(p, 1);
797     data1_element *e;
798
799     if (!root)
800         return 0;
801         
802     for (e = data1_absyn_getelements(p->dh, root); e; e=e->next)
803     {
804         data1_tag *tag = e->tag;
805         
806         if (tag && tag->which == DATA1T_string &&
807             !yaz_matchstr(tag->value.string, "mc?"))
808                 parse_data1_tree(p, tag->value.string, root);
809     }
810     return root;
811 }
812
813 data1_node *grs_read_marc(struct grs_read_info *p)
814 {
815     data1_node *root = grs_read_iso2709(p, 0);
816     data1_element *e;
817
818     if (!root)
819         return 0;
820         
821     for (e = data1_absyn_getelements(p->dh, root); e; e=e->next)
822     {
823         data1_tag *tag = e->tag;
824         
825         if (tag && tag->which == DATA1T_string &&
826             !yaz_matchstr(tag->value.string, "mc?"))
827                 parse_data1_tree(p, tag->value.string, root);
828     }
829     return root;
830 }
831
832 static void *init_marc(Res res, RecType rt)
833 {
834     struct marc_info *p = xmalloc(sizeof(*p));
835     strcpy(p->type, "");
836     return p;
837 }
838
839 static ZEBRA_RES config_marc(void *clientData, Res res, const char *args)
840 {
841     struct marc_info *p = (struct marc_info*) clientData;
842     if (strlen(args) < sizeof(p->type))
843         strcpy(p->type, args);
844     return ZEBRA_OK;
845 }
846
847 static void destroy_marc(void *clientData)
848 {
849     struct marc_info *p = (struct marc_info*) clientData;
850     xfree (p);
851 }
852
853
854 static int extract_marc(void *clientData, struct recExtractCtrl *ctrl)
855 {
856     return zebra_grs_extract(clientData, ctrl, grs_read_marc);
857 }
858
859 static int retrieve_marc(void *clientData, struct recRetrieveCtrl *ctrl)
860 {
861     return zebra_grs_retrieve(clientData, ctrl, grs_read_marc);
862 }
863
864 static struct recType marc_type = {
865     0,
866     "grs.marc",
867     init_marc,
868     config_marc,
869     destroy_marc,
870     extract_marc,
871     retrieve_marc,
872 };
873
874 static int extract_marcxml(void *clientData, struct recExtractCtrl *ctrl)
875 {
876     return zebra_grs_extract(clientData, ctrl, grs_read_marcxml);
877 }
878
879 static int retrieve_marcxml(void *clientData, struct recRetrieveCtrl *ctrl)
880 {
881     return zebra_grs_retrieve(clientData, ctrl, grs_read_marcxml);
882 }
883
884 static struct recType marcxml_type = {
885     0,
886     "grs.marcxml",
887     init_marc,
888     config_marc,
889     destroy_marc,
890     extract_marcxml,
891     retrieve_marcxml,
892 };
893
894 RecType
895 #ifdef IDZEBRA_STATIC_GRS_MARC
896 idzebra_filter_grs_marc
897 #else
898 idzebra_filter
899 #endif
900
901 [] = {
902     &marc_type,
903     &marcxml_type,
904     0,
905 };
906     
907 /*
908  * Local variables:
909  * c-basic-offset: 4
910  * indent-tabs-mode: nil
911  * End:
912  * vim: shiftwidth=4 tabstop=8 expandtab
913  */
914