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