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