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