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