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