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