2 * Copyright (c) 1995, Index Data.
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
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.
11 * Revision 1.11 1996/07/06 19:58:35 quinn
12 * System headerfiles gathered in yconfig
14 * Revision 1.10 1996/01/19 15:41:47 quinn
15 * Fixed uninitialized boolean.
17 * Revision 1.9 1996/01/17 14:52:47 adam
18 * Changed prototype for reader function parsed to data1_read_record.
20 * Revision 1.8 1995/12/15 16:20:41 quinn
21 * Added formatted text.
23 * Revision 1.7 1995/12/13 13:44:32 quinn
24 * Modified Data1-system to use nmem
26 * Revision 1.6 1995/12/12 16:37:08 quinn
27 * Added destroy element to data1_node.
29 * Revision 1.5 1995/12/11 15:22:37 quinn
30 * Added last_child field to the node.
31 * Rewrote schema-mapping.
33 * Revision 1.4 1995/11/13 09:27:36 quinn
34 * Fiddling with the variant stuff.
36 * Revision 1.3 1995/11/01 16:34:57 quinn
37 * Making data1 look for tables in data1_tabpath
39 * Revision 1.2 1995/11/01 13:54:48 quinn
42 * Revision 1.1 1995/11/01 11:56:09 quinn
43 * Added Retrieval (data management) functions en masse.
45 * Revision 1.14 1995/10/30 12:40:55 quinn
46 * Fixed a couple of bugs.
48 * Revision 1.13 1995/10/25 16:00:47 quinn
49 * USMARC support is now almost operational
51 * Revision 1.12 1995/10/16 14:02:55 quinn
52 * Changes to support element set names and espec1
54 * Revision 1.11 1995/10/13 16:05:08 quinn
55 * Adding Espec1-processing
57 * Revision 1.10 1995/10/11 14:53:44 quinn
60 * Revision 1.9 1995/10/06 16:56:50 quinn
61 * Fixed ranked result.
63 * Revision 1.8 1995/10/06 16:44:13 quinn
64 * Work on attribute set mapping, etc.
66 * Revision 1.7 1995/10/06 12:58:35 quinn
69 * Revision 1.6 1995/10/04 09:29:49 quinn
70 * Adjustments to support USGS test data
72 * Revision 1.5 1995/10/03 17:56:43 quinn
75 * Revision 1.4 1995/10/02 15:53:19 quinn
78 * Revision 1.3 1995/10/02 14:55:21 quinn
79 * *** empty log message ***
81 * Revision 1.2 1995/09/14 15:18:13 quinn
84 * Revision 1.1 1995/09/12 11:24:30 quinn
85 * Beginning to add code for structured records.
98 char *data1_tabpath = 0; /* global path for tables */
100 void data1_set_tabpath(char *p)
101 { data1_tabpath = p; }
104 static data1_node *freelist = 0;
108 * get the tag which is the immediate parent of this node (this may mean
109 * traversing intermediate things like variants and stuff.
111 data1_node *get_parent_tag(data1_node *n)
113 for (; n && n->which != DATA1N_root; n = n->parent)
114 if (n->which == DATA1N_tag)
119 data1_node *data1_mk_node(NMEM m)
127 if (!(r = xmalloc(sizeof(*r))))
130 r = nmem_malloc(m, sizeof(*r));
132 r->next = r->child = r->last_child = r->parent = 0;
139 static void fr_node(data1_node *n)
146 void data1_free_tree(data1_node *t)
148 data1_node *p = t->child, *pn;
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.
168 data1_node *data1_insert_taggeddata(data1_node *root, data1_node *at,
169 char *tagname, NMEM m)
171 data1_node *tagn = data1_mk_node(m);
174 tagn->which = DATA1N_tag;
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,
184 tagn->child = datn = data1_mk_node(m);
185 tagn->num_children = 1;
188 datn->which = DATA1N_data;
189 datn->u.data.formatted_text = 0;
190 tagn->next = at->child;
198 * Ugh. Sometimes functions just grow and grow on you. This one reads a
199 * 'node' and its children.
201 data1_node *data1_read_node(char **buf, data1_node *parent, int *line,
202 data1_absyn *absyn, NMEM m)
206 while (**buf && isspace(**buf))
215 if (**buf == '<') /* beginning of tag */
217 char *tag = (*buf) + 1;
221 data1_element *elem = 0;
223 for (; *t && *t != '>' && !isspace(*t); t++);
224 if (*t != '>' && !isspace(*t))
226 logf(LOG_WARN, "d1: %d: Malformed tag", *line);
229 if (isspace(*t)) /* the tag has arguments */
236 for (; *t && *t != '>'; t++);
237 if (*t != '>' && !isspace(*t))
239 logf(LOG_WARN, "d1: %d: Malformed tag", *line);
246 * if end-tag, see if we terminate parent. If so, consume and return.
254 if (!*(tag +1) || (parent->which == DATA1N_root && !strcmp(tag + 1,
255 parent->u.root.type)) ||
256 (parent->which == DATA1N_tag && !strcmp(tag + 1,
269 if (!absyn) /* parent node - what are we? */
271 if (!(absyn = data1_get_absyn(tag)))
273 logf(LOG_WARN, "Unable to acquire abstract syntax for '%s'",
277 res = data1_mk_node(m);
278 res->which = DATA1N_root;
279 res->u.root.type = tag;
280 res->u.root.absyn = absyn;
284 else if (!strncmp(tag, "var", 3))
286 char class[DATA1_MAX_SYMBOL], type[DATA1_MAX_SYMBOL];
291 if (sscanf(args, "%s %s %n", class, type, &val_offset) != 2)
293 logf(LOG_WARN, "Malformed variant triple at '%s'", tag);
296 if (!(tp = data1_getvartypebyct(parent->root->u.root.absyn->varset,
301 * If we're the first variant in this group, create a parent var,
302 * and insert it before the current variant.
304 if (parent->which != DATA1N_variant)
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;
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.
322 for (p = parent; p->which == DATA1N_variant; p = p->parent)
323 if (p->u.variant.type == tp)
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;
337 else /* tag.. acquire our element in the abstract syntax */
339 data1_node *partag = get_parent_tag(parent);
340 data1_element *e = 0;
343 if (parent->which == DATA1N_variant)
349 if (!(e = partag->u.tag.element))
350 localtag = 1; /* our parent is a local tag */
353 if (!localtag && !(elem = data1_getelementbytagname(absyn,
354 e, tag)) && (data1_gettagbyname(absyn->tagset, tag)))
356 if (parent->which == DATA1N_root)
357 logf(LOG_WARN, "Tag '%s' used out of context", tag);
362 elem = data1_getelementbytagname(absyn, e, tag);
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;
376 res->parent = parent;
377 res->num_children = 0;
383 while ((*pp = data1_read_node(buf, res, line, absyn, m)))
385 res->last_child = *pp;
390 else /* != '<'... this is a body of text */
393 char *data = *buf, *pp = *buf;
395 data1_node *partag = get_parent_tag(parent);
398 if (!parent) /* abort if abstract syntax is undefined */
400 /* Determine length and remove newlines/extra blanks */
401 while (**buf && **buf != '<')
409 while (isspace(**buf))
413 *(pp++) = *((*buf)++);
416 while (isspace(data[len-1]))
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;
431 * Read a record in the native syntax.
433 data1_node *data1_read_record(int (*rf)(void *, char *, size_t), void *fh,
436 static char *buf = 0;
442 if (!buf && !(buf = xmalloc(size = 4096)))
447 if (rd + 4096 > size && !(buf =xrealloc(buf, size *= 2)))
449 if ((res = (*rf)(fh, buf + rd, 4096)) <= 0)
454 return data1_read_node(&bp, 0, &line, 0, m);