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