YAZ Build date for WIN32.
[yaz-moved-to-github.git] / retrieval / d1_read.c
1 /*
2  * Copyright (c) 1995-2001, Index Data.
3  * See the file LICENSE for details.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: d1_read.c,v $
7  * Revision 1.38  2001-03-27 23:06:21  adam
8  * Quotes and slashes may occur within attributes.
9  *
10  * Revision 1.37  2001/02/28 09:00:06  adam
11  * Fixed problem with stack overflow for very nested records.
12  *
13  * Revision 1.36  2001/02/21 13:46:53  adam
14  * C++ fixes.
15  *
16  * Revision 1.35  2000/12/05 14:44:25  adam
17  * Readers skips <! ...> sections.
18  *
19  * Revision 1.34  2000/12/05 10:06:23  adam
20  * Added support for null-data rules like <tag/>.
21  *
22  * Revision 1.33  2000/11/29 14:22:47  adam
23  * Implemented XML/SGML attributes for data1 so that d1_read reads them
24  * and d1_write generates proper attributes for XML/SGML records. Added
25  * register locking for threaded version.
26  *
27  * Revision 1.32  2000/01/06 11:25:59  adam
28  * Added case to prevent warning.
29  *
30  * Revision 1.31  1999/12/21 14:16:20  ian
31  * Changed retrieval module to allow data1 trees with no associated absyn.
32  * Also added a simple interface for extracting values from data1 trees using
33  * a string based tagpath.
34  *
35  * Revision 1.30  1999/11/30 13:47:12  adam
36  * Improved installation. Moved header files to include/yaz.
37  *
38  * Revision 1.29  1999/10/21 12:06:29  adam
39  * Retrieval module no longer uses ctype.h - functions.
40  *
41  * Revision 1.28  1999/10/21 09:50:33  adam
42  * SGML reader uses own isspace - it doesn't do 8-bit on WIN32!
43  *
44  * Revision 1.27  1999/08/27 09:40:32  adam
45  * Renamed logf function to yaz_log. Removed VC++ project files.
46  *
47  * Revision 1.26  1999/07/13 13:23:48  adam
48  * Non-recursive version of data1_read_node. data1_read_nodex reads
49  * stream of bytes (instead of buffer in memory).
50  *
51  * Revision 1.25  1999/04/20 09:56:48  adam
52  * Added 'name' paramter to encoder/decoder routines (typedef Odr_fun).
53  * Modified all encoders/decoders to reflect this change.
54  *
55  * Revision 1.24  1998/10/28 15:10:09  adam
56  * Added --with-yc option to configure. For the data1_node in data1.h:
57  * decreased size of localdata and removed member "line" which wasn't useful.
58  *
59  * Revision 1.23  1998/03/12 11:28:45  adam
60  * Fix: didn't set root member of tagged node in function.
61  * data1_add_insert_taggeddata.
62  *
63  * Revision 1.22  1998/03/05 08:15:32  adam
64  * Implemented data1_add_insert_taggeddata utility which is more flexible
65  * than data1_insert_taggeddata.
66  *
67  * Revision 1.21  1998/02/27 14:08:05  adam
68  * Added const to some char pointer arguments.
69  * Reworked data1_read_node so that it doesn't create a tree with
70  * pointers to original "SGML"-buffer.
71  *
72  * Revision 1.20  1998/02/11 11:53:35  adam
73  * Changed code so that it compiles as C++.
74  *
75  * Revision 1.19  1997/12/09 16:17:09  adam
76  * Fix bug regarding variants. Tags with prefix "var" was incorrectly
77  * interpreted as "start of variants". Now, only "var" indicates such
78  * start.
79  * Cleaned up data1_read_node so tag names and variant names are
80  * copied and not pointed to by the generated data1 tree. Data nodes
81  * still point to old buffer.
82  *
83  * Revision 1.18  1997/11/18 09:51:09  adam
84  * Removed element num_children from data1_node. Minor changes in
85  * data1 to Explain.
86  *
87  * Revision 1.17  1997/11/05 09:20:51  adam
88  * Minor change.
89  *
90  * Revision 1.16  1997/09/17 12:10:37  adam
91  * YAZ version 1.4.
92  *
93  * Revision 1.15  1997/09/05 09:50:57  adam
94  * Removed global data1_tabpath - uses data1_get_tabpath() instead.
95  *
96  * Revision 1.14  1997/05/14 06:54:04  adam
97  * C++ support.
98  *
99  * Revision 1.13  1996/10/29 13:35:38  adam
100  * Implemented data1_set_tabpath and data1_get_tabpath.
101  *
102  * Revision 1.12  1996/10/11 10:35:38  adam
103  * Fixed a bug that caused data1_read_node to core dump when no abstract
104  * syntax was defined in a "sgml"-record.
105  *
106  * Revision 1.11  1996/07/06 19:58:35  quinn
107  * System headerfiles gathered in yconfig
108  *
109  * Revision 1.10  1996/01/19  15:41:47  quinn
110  * Fixed uninitialized boolean.
111  *
112  * Revision 1.9  1996/01/17  14:52:47  adam
113  * Changed prototype for reader function parsed to data1_read_record.
114  *
115  * Revision 1.8  1995/12/15  16:20:41  quinn
116  * Added formatted text.
117  *
118  * Revision 1.7  1995/12/13  13:44:32  quinn
119  * Modified Data1-system to use nmem
120  *
121  * Revision 1.6  1995/12/12  16:37:08  quinn
122  * Added destroy element to data1_node.
123  *
124  * Revision 1.5  1995/12/11  15:22:37  quinn
125  * Added last_child field to the node.
126  * Rewrote schema-mapping.
127  *
128  * Revision 1.4  1995/11/13  09:27:36  quinn
129  * Fiddling with the variant stuff.
130  *
131  * Revision 1.3  1995/11/01  16:34:57  quinn
132  * Making data1 look for tables in data1_tabpath
133  *
134  * Revision 1.2  1995/11/01  13:54:48  quinn
135  * Minor adjustments
136  *
137  * Revision 1.1  1995/11/01  11:56:09  quinn
138  * Added Retrieval (data management) functions en masse.
139  *
140  * Revision 1.14  1995/10/30  12:40:55  quinn
141  * Fixed a couple of bugs.
142  *
143  * Revision 1.13  1995/10/25  16:00:47  quinn
144  * USMARC support is now almost operational
145  *
146  * Revision 1.12  1995/10/16  14:02:55  quinn
147  * Changes to support element set names and espec1
148  *
149  * Revision 1.11  1995/10/13  16:05:08  quinn
150  * Adding Espec1-processing
151  *
152  * Revision 1.10  1995/10/11  14:53:44  quinn
153  * Work on variants.
154  *
155  * Revision 1.9  1995/10/06  16:56:50  quinn
156  * Fixed ranked result.
157  *
158  * Revision 1.8  1995/10/06  16:44:13  quinn
159  * Work on attribute set mapping, etc.
160  *
161  * Revision 1.7  1995/10/06  12:58:35  quinn
162  * SUTRS support
163  *
164  * Revision 1.6  1995/10/04  09:29:49  quinn
165  * Adjustments to support USGS test data
166  *
167  * Revision 1.5  1995/10/03  17:56:43  quinn
168  * Fixing GRS code.
169  *
170  * Revision 1.4  1995/10/02  15:53:19  quinn
171  * Work
172  *
173  * Revision 1.3  1995/10/02  14:55:21  quinn
174  * *** empty log message ***
175  *
176  * Revision 1.2  1995/09/14  15:18:13  quinn
177  * Work
178  *
179  * Revision 1.1  1995/09/12  11:24:30  quinn
180  * Beginning to add code for structured records.
181  *
182  *
183  */
184
185 #include <assert.h>
186 #include <stdio.h>
187 #include <stdlib.h>
188
189 #include <yaz/xmalloc.h>
190 #include <yaz/log.h>
191 #include <yaz/data1.h>
192
193 /*
194  * get the tag which is the immediate parent of this node (this may mean
195  * traversing intermediate things like variants and stuff.
196  */
197 data1_node *get_parent_tag (data1_handle dh, data1_node *n)
198 {
199     for (; n && n->which != DATA1N_root; n = n->parent)
200         if (n->which == DATA1N_tag)
201             return n;
202     return 0;
203 }
204
205 data1_node *data1_mk_node (data1_handle dh, NMEM m)
206 {
207     data1_node *r;
208
209     r = (data1_node *)nmem_malloc(m, sizeof(*r));
210     r->next = r->child = r->last_child = r->parent = 0;
211     r->destroy = 0;
212     return r;
213 }
214
215 data1_node *data1_mk_node_type (data1_handle dh, NMEM m, int type)
216 {
217     data1_node *r;
218
219     r = data1_mk_node(dh, m);
220     r->which = type;
221     switch(type)
222     {
223     case DATA1N_tag:
224         r->u.tag.tag = 0;
225         r->u.tag.element = 0;
226         r->u.tag.no_data_requested = 0;
227         r->u.tag.node_selected = 0;
228         r->u.tag.make_variantlist = 0;
229         r->u.tag.get_bytes = -1;
230 #if DATA1_USING_XATTR
231         r->u.tag.attributes = 0;
232 #endif
233         break;
234     case DATA1N_root:
235         r->u.root.type = 0;
236         r->u.root.absyn = 0;
237         break;
238     case DATA1N_data:
239         r->u.data.data = 0;
240         r->u.data.len = 0;
241         r->u.data.what = 0;
242         r->u.data.formatted_text = 0;
243         break;
244     default:
245         logf (LOG_WARN, "data_mk_node_type. bad type = %d\n", type);
246     }
247     return r;
248 }
249
250 void data1_free_tree (data1_handle dh, data1_node *t)
251 {
252     data1_node *p = t->child, *pn;
253
254     while (p)
255     {
256         pn = p->next;
257         data1_free_tree (dh, p);
258         p = pn;
259     }
260     if (t->destroy)
261         (*t->destroy)(t);
262 }
263
264 char *data1_insert_string (data1_handle dh, data1_node *res,
265                            NMEM m, const char *str)
266 {
267     int len = strlen(str);
268
269     if (len >= DATA1_LOCALDATA)
270         return nmem_strdup (m, str);
271     else
272     {
273         strcpy (res->lbuf, str);
274         return res->lbuf;
275     }
276 }
277
278 data1_node *data1_add_insert_taggeddata(data1_handle dh, data1_node *root,
279                                         data1_node *at,
280                                         const char *tagname, NMEM m,
281                                         int first_flag, int local_allowed)
282 {
283     data1_node *partag = get_parent_tag (dh, at);
284     data1_node *tagn = data1_mk_node_type (dh, m, DATA1N_tag);
285     data1_element *e = NULL;
286     data1_node *datn;
287
288     tagn->u.tag.tag = data1_insert_string (dh, tagn, m, tagname);
289
290     if (partag)
291         e = partag->u.tag.element;
292     tagn->u.tag.element =
293         data1_getelementbytagname (dh, root->u.root.absyn, e, tagname);
294     if (!local_allowed && !tagn->u.tag.element)
295         return NULL;
296     tagn->last_child = tagn->child = datn = data1_mk_node_type (dh, m, DATA1N_data);
297     tagn->root = root;
298     datn->parent = tagn;
299     datn->root = root;
300     datn->u.data.formatted_text = 0;
301     tagn->parent = at;
302
303     if (first_flag)
304     {
305         tagn->next = at->child;
306         if (!tagn->next)
307             at->last_child = tagn;
308         at->child = tagn;
309     }
310     else
311     {
312         if (!at->child)
313             at->child = tagn;
314         else
315         {
316             assert (at->last_child);
317             at->last_child->next = tagn;
318         }
319         at->last_child = tagn;
320     }
321     return datn;
322 }
323
324 data1_node *data1_add_taggeddata(data1_handle dh, data1_node *root,
325                                  data1_node *at,
326                                  const char *tagname, NMEM m)
327 {
328     return data1_add_insert_taggeddata (dh, root, at, tagname, m, 0, 1);
329 }
330
331
332 /*
333  * Insert a tagged node into the record root as first child of the node at
334  * which should be root or tag itself). Returns pointer to the data node,
335  * which can then be modified.
336  */
337 data1_node *data1_insert_taggeddata(data1_handle dh, data1_node *root,
338                                     data1_node *at,
339                                     const char *tagname, NMEM m)
340 {
341     return data1_add_insert_taggeddata (dh, root, at, tagname, m, 1, 0);
342 }
343
344 #if DATA1_USING_XATTR
345 data1_xattr *data1_read_xattr (data1_handle dh, NMEM m,
346                                int (*get_byte)(void *fh), void *fh,
347                                WRBUF wrbuf, int *ch)
348 {
349     data1_xattr *p_first = 0;
350     data1_xattr **pp = &p_first;
351     int c = *ch;
352     for (;;)
353     {
354         data1_xattr *p;
355         int len;
356         while (c && d1_isspace(c))
357             c = (*get_byte)(fh);
358         if (!c  || c == '>' || c == '/')
359             break;
360         *pp = p = (data1_xattr *) nmem_malloc (m, sizeof(*p));
361         p->next = 0;
362         pp = &p->next;
363         p->value = 0;
364         
365         wrbuf_rewind(wrbuf);
366         while (c && c != '=' && c != '>' && c != '/' && !d1_isspace(c))
367         {
368             wrbuf_putc (wrbuf, c);
369             c = (*get_byte)(fh);
370         }
371         wrbuf_putc (wrbuf, '\0');
372         len = wrbuf_len(wrbuf);
373         p->name = (char*) nmem_malloc (m, len);
374         strcpy (p->name, wrbuf_buf(wrbuf));
375         if (c == '=')
376         {
377             c = (*get_byte)(fh);
378             if (c == '"')
379             {
380                 c = (*get_byte)(fh);    
381                 wrbuf_rewind(wrbuf);
382                 while (c && c != '"')
383                 {
384                     wrbuf_putc (wrbuf, c);
385                     c = (*get_byte)(fh);
386                 }
387                 if (c)
388                     c = (*get_byte)(fh);        
389             }
390             else
391             {
392                 wrbuf_rewind(wrbuf);
393                 while (c && c != '>' && c != '/')
394                 {
395                     wrbuf_putc (wrbuf, c);
396                     c = (*get_byte)(fh);
397                 }
398             }
399             wrbuf_putc (wrbuf, '\0');
400             len = wrbuf_len(wrbuf);
401             p->value = (char*) nmem_malloc (m, len);
402             strcpy (p->value, wrbuf_buf(wrbuf));
403         }
404     }
405     *ch = c;
406     return p_first;
407 }
408 #endif
409
410 /*
411  * Ugh. Sometimes functions just grow and grow on you. This one reads a
412  * 'node' and its children.
413  */
414 data1_node *data1_read_nodex (data1_handle dh, NMEM m,
415                               int (*get_byte)(void *fh), void *fh, WRBUF wrbuf)
416 {
417     data1_absyn *absyn = 0;
418     data1_node *d1_stack[256];
419     data1_node *res;
420     int c;
421     int level = 0;
422     int line = 1;
423
424     d1_stack[level] = 0;
425     c = (*get_byte)(fh);
426     while (1)
427     {
428         data1_node *parent = level ? d1_stack[level-1] : 0;
429         while (c != '\0' && d1_isspace(c))
430         {
431             if (c == '\n')
432                 line++;
433             c = (*get_byte)(fh);
434         }
435         if (c == '\0')
436             break;
437         
438         if (c == '<') /* beginning of tag */
439         {
440 #if DATA1_USING_XATTR
441             data1_xattr *xattr;
442 #endif
443             char tag[64];
444             char args[256];
445             int null_tag = 0;
446             int end_tag = 0;
447             size_t i = 0;
448
449             c = (*get_byte)(fh);
450             if (c == '/')
451             {
452                 end_tag = 1;
453                 c = (*get_byte)(fh);
454             }
455             else if (c == '!')  /* tags/comments that we don't deal with yet */
456             {
457                 while (c && c != '>')
458                     c = (*get_byte)(fh);
459                 if (c)
460                     c = (*get_byte)(fh);
461                 continue;
462             }
463             while (c && c != '>' && c != '/' && !d1_isspace(c))
464             {
465                 if (i < (sizeof(tag)-1))
466                     tag[i++] = c;
467                 c = (*get_byte)(fh);
468             }
469             tag[i] = '\0';
470 #if DATA1_USING_XATTR
471             xattr = data1_read_xattr (dh, m, get_byte, fh, wrbuf, &c);
472             args[0] = '\0';
473 #else
474             while (d1_isspace(c))
475                 c = (*get_byte)(fh);
476             for (i = 0; c && c != '>' && c != '/'; c = (*get_byte)(fh))
477                 if (i < (sizeof(args)-1))
478                     args[i++] = c;
479             args[i] = '\0';
480 #endif
481             if (c == '/')
482             {    /* <tag attrs/> or <tag/> */
483                 null_tag = 1;
484                 c = (*get_byte)(fh);
485             }
486             if (c != '>')
487             {
488                 yaz_log(LOG_WARN, "d1: %d: Malformed tag", line);
489                 return 0;
490             }
491             else
492                 c = (*get_byte)(fh);
493
494             /* End tag? */
495             if (end_tag)       
496             {
497                 if (*tag == '\0')
498                     --level;        /* </> */
499                 else
500                 {                   /* </tag> */
501                     int i = level;
502                     while (i > 0)
503                     {
504                         parent = d1_stack[--i];
505                         if ((parent->which == DATA1N_root &&
506                              !strcmp(tag, parent->u.root.type)) ||
507                             (parent->which == DATA1N_tag &&
508                              !strcmp(tag, parent->u.tag.tag)))
509                         {
510                             level = i;
511                             break;
512                         }
513                     }
514                     if (i != level)
515                     {
516                         yaz_log (LOG_WARN, "%d: no begin tag for %s",
517                                  line, tag);
518                         break;
519                     }
520                 }
521                 if (level == 0)
522                     return d1_stack[0];
523                 continue;
524             }   
525             if (level == 0) /* root ? */
526             {
527                 if (!(absyn = data1_get_absyn (dh, tag)))
528                 {
529                     yaz_log(LOG_WARN, "Unable to acquire abstract syntax " "for '%s'", tag); 
530                     /* It's now OK for a record not to have an absyn */
531                 }
532                 res = data1_mk_node_type (dh, m, DATA1N_root);
533                 res->u.root.type = data1_insert_string (dh, res, m, tag);
534                 res->u.root.absyn = absyn;
535                 res->root = res;
536             }
537             else if (!strcmp(tag, "var"))
538             {
539                 char tclass[DATA1_MAX_SYMBOL], type[DATA1_MAX_SYMBOL];
540                 data1_vartype *tp;
541                 int val_offset;
542                 
543                 if (sscanf(args, "%s %s %n", tclass, type, &val_offset) != 2)
544                 {
545                     yaz_log(LOG_WARN, "Malformed variant triple at '%s'", tag);
546                     continue;
547                 }
548                 if (!(tp =
549                       data1_getvartypebyct(dh,
550                                            parent->root->u.root.absyn->varset,
551                                            tclass, type)))
552                     continue;
553                 /*
554                  * If we're the first variant in this group, create a parent 
555                  * variant, and insert it before the current variant.
556                  */
557                 if (parent->which != DATA1N_variant)
558                 {
559                     res = data1_mk_node (dh, m);
560                     res->which = DATA1N_variant;
561                     res->u.variant.type = 0;
562                     res->u.variant.value = 0;
563                 }
564                 else
565                 {
566                     /*
567                      * now determine if one of our ancestor triples is of
568                      * same type. If so, we break here.
569                      */
570                     int i;
571                     for (i = level-1; d1_stack[i]->which==DATA1N_variant; --i)
572                         if (d1_stack[i]->u.variant.type == tp)
573                         {
574                             level = i;
575                             break;
576                         }
577                     res = data1_mk_node (dh, m);
578                     res->which = DATA1N_variant;
579                     res->u.variant.type = tp;
580                     res->u.variant.value =
581                         data1_insert_string (dh, res, m, args + val_offset);
582                 }
583             }
584             else /* tag.. acquire our element in the abstract syntax */
585             {
586                 data1_node *partag = get_parent_tag (dh, parent);
587                 data1_element *elem, *e = 0;
588                 int localtag = 0;
589                 
590                 if (parent->which == DATA1N_variant)
591                     return 0;
592                 if (partag)
593                     if (!(e = partag->u.tag.element))
594                         localtag = 1; /* our parent is a local tag */
595                 
596                 elem = data1_getelementbytagname(dh, absyn, e, tag);
597                 res = data1_mk_node_type (dh, m, DATA1N_tag);
598                 res->u.tag.tag = data1_insert_string (dh, res, m, tag);
599                 res->u.tag.element = elem;
600 #if DATA1_USING_XATTR
601                 res->u.tag.attributes = xattr;
602 #endif
603             }
604             if (parent)
605             {
606                 parent->last_child = res;
607                 res->root = parent->root;
608             }
609             res->parent = parent;
610             if (d1_stack[level])
611                 d1_stack[level]->next = res;
612             else if (parent)
613                 parent->child = res;
614             d1_stack[level] = res;
615             d1_stack[level+1] = 0;
616             if (level < 250 && !null_tag)
617                 ++level;
618         }
619         else /* != '<'... this is a body of text */
620         {
621             const char *src;
622             char *dst;
623             int len, prev_char = 0;
624             
625             if (level == 0)
626             {
627                 c = (*get_byte)(fh);
628                 continue;
629             }
630             res = data1_mk_node_type (dh, m, DATA1N_data);
631             res->parent = parent;
632             res->u.data.what = DATA1I_text;
633             res->u.data.formatted_text = 0;
634             res->root = parent->root;
635             parent->last_child = res;
636             if (d1_stack[level])
637                 d1_stack[level]->next = res;
638             else
639                 parent->child = res;
640             d1_stack[level] = res;
641             
642             wrbuf_rewind(wrbuf);
643
644             while (c && c != '<')
645             {
646                 wrbuf_putc (wrbuf, c);
647                 c = (*get_byte)(fh);
648             }
649             len = wrbuf_len(wrbuf);
650
651             /* use local buffer of nmem if too large */
652             if (len >= DATA1_LOCALDATA)
653                 res->u.data.data = (char*) nmem_malloc (m, len);
654             else
655                 res->u.data.data = res->lbuf;
656             
657             /* read "data" and transfer while removing white space */
658             dst = res->u.data.data;
659             for (src = wrbuf_buf(wrbuf); --len >= 0; src++)
660             {
661                 if (*src == '\n')
662                     line++;
663                 if (d1_isspace (*src))
664                     prev_char = ' ';
665                 else
666                 {
667                     if (prev_char)
668                     {
669                         *dst++ = prev_char;
670                         prev_char = 0;
671                     }
672                     *dst++ = *src;
673                 }
674             }
675             res->u.data.len = dst - res->u.data.data;
676         }
677     }
678     return 0;
679 }
680
681 int getc_mem (void *fh)
682 {
683     const char **p = (const char **) fh;
684     if (**p)
685         return *(*p)++;
686     return 0;
687 }
688
689 data1_node *data1_read_node (data1_handle dh, const char **buf, NMEM m)
690 {
691     WRBUF wrbuf = wrbuf_alloc();
692     data1_node *node;
693
694     node = data1_read_nodex(dh, m, getc_mem, (void *) (buf), wrbuf);
695     wrbuf_free (wrbuf, 1);
696     return node;
697 }
698
699 /*
700  * Read a record in the native syntax.
701  */
702 data1_node *data1_read_record(data1_handle dh,
703                               int (*rf)(void *, char *, size_t), void *fh,
704                               NMEM m)
705 {
706     int *size;
707     char **buf = data1_get_read_buf (dh, &size);
708     const char *bp;
709     int rd = 0, res;
710     
711     if (!*buf)
712         *buf = (char *)xmalloc(*size = 4096);
713     
714     for (;;)
715     {
716         if (rd + 2048 >= *size && !(*buf =(char *)xrealloc(*buf, *size *= 2)))
717             abort();
718         if ((res = (*rf)(fh, *buf + rd, 2048)) <= 0)
719         {
720             if (!res)
721             {
722                 bp = *buf;
723                 (*buf)[rd] = '\0';
724                 return data1_read_node(dh, &bp, m);
725             }
726             else
727                 return 0;
728         }
729         rd += res;
730     }
731 }
732
733 data1_node *data1_read_sgml (data1_handle dh, NMEM m, const char *buf)
734 {
735     const char *bp = buf;
736     return data1_read_node (dh, &bp, m);
737 }
738