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