2 * Copyright (c) 1995, Index Data.
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.14 1997-05-14 06:54:04 adam
10 * Revision 1.13 1996/10/29 13:35:38 adam
11 * Implemented data1_set_tabpath and data1_get_tabpath.
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.
17 * Revision 1.11 1996/07/06 19:58:35 quinn
18 * System headerfiles gathered in yconfig
20 * Revision 1.10 1996/01/19 15:41:47 quinn
21 * Fixed uninitialized boolean.
23 * Revision 1.9 1996/01/17 14:52:47 adam
24 * Changed prototype for reader function parsed to data1_read_record.
26 * Revision 1.8 1995/12/15 16:20:41 quinn
27 * Added formatted text.
29 * Revision 1.7 1995/12/13 13:44:32 quinn
30 * Modified Data1-system to use nmem
32 * Revision 1.6 1995/12/12 16:37:08 quinn
33 * Added destroy element to data1_node.
35 * Revision 1.5 1995/12/11 15:22:37 quinn
36 * Added last_child field to the node.
37 * Rewrote schema-mapping.
39 * Revision 1.4 1995/11/13 09:27:36 quinn
40 * Fiddling with the variant stuff.
42 * Revision 1.3 1995/11/01 16:34:57 quinn
43 * Making data1 look for tables in data1_tabpath
45 * Revision 1.2 1995/11/01 13:54:48 quinn
48 * Revision 1.1 1995/11/01 11:56:09 quinn
49 * Added Retrieval (data management) functions en masse.
51 * Revision 1.14 1995/10/30 12:40:55 quinn
52 * Fixed a couple of bugs.
54 * Revision 1.13 1995/10/25 16:00:47 quinn
55 * USMARC support is now almost operational
57 * Revision 1.12 1995/10/16 14:02:55 quinn
58 * Changes to support element set names and espec1
60 * Revision 1.11 1995/10/13 16:05:08 quinn
61 * Adding Espec1-processing
63 * Revision 1.10 1995/10/11 14:53:44 quinn
66 * Revision 1.9 1995/10/06 16:56:50 quinn
67 * Fixed ranked result.
69 * Revision 1.8 1995/10/06 16:44:13 quinn
70 * Work on attribute set mapping, etc.
72 * Revision 1.7 1995/10/06 12:58:35 quinn
75 * Revision 1.6 1995/10/04 09:29:49 quinn
76 * Adjustments to support USGS test data
78 * Revision 1.5 1995/10/03 17:56:43 quinn
81 * Revision 1.4 1995/10/02 15:53:19 quinn
84 * Revision 1.3 1995/10/02 14:55:21 quinn
85 * *** empty log message ***
87 * Revision 1.2 1995/09/14 15:18:13 quinn
90 * Revision 1.1 1995/09/12 11:24:30 quinn
91 * Beginning to add code for structured records.
104 char *data1_tabpath = 0; /* global path for tables */
106 void data1_set_tabpath(const char *p)
110 xfree (data1_tabpath);
111 data1_tabpath = NULL;
115 data1_tabpath = xmalloc (strlen(p)+1);
116 strcpy (data1_tabpath, p);
120 const char *data1_get_tabpath (void)
122 return data1_tabpath;
126 static data1_node *freelist = 0;
130 * get the tag which is the immediate parent of this node (this may mean
131 * traversing intermediate things like variants and stuff.
133 data1_node *get_parent_tag(data1_node *n)
135 for (; n && n->which != DATA1N_root; n = n->parent)
136 if (n->which == DATA1N_tag)
141 data1_node *data1_mk_node(NMEM m)
149 if (!(r = xmalloc(sizeof(*r))))
152 r = nmem_malloc(m, sizeof(*r));
154 r->next = r->child = r->last_child = r->parent = 0;
161 static void fr_node(data1_node *n)
168 void data1_free_tree(data1_node *t)
170 data1_node *p = t->child, *pn;
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.
190 data1_node *data1_insert_taggeddata(data1_node *root, data1_node *at,
191 char *tagname, NMEM m)
193 data1_node *tagn = data1_mk_node(m);
196 tagn->which = DATA1N_tag;
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,
206 tagn->child = datn = data1_mk_node(m);
207 tagn->num_children = 1;
210 datn->which = DATA1N_data;
211 datn->u.data.formatted_text = 0;
212 tagn->next = at->child;
220 * Ugh. Sometimes functions just grow and grow on you. This one reads a
221 * 'node' and its children.
223 data1_node *data1_read_node(char **buf, data1_node *parent, int *line,
224 data1_absyn *absyn, NMEM m)
228 while (**buf && isspace(**buf))
237 if (**buf == '<') /* beginning of tag */
239 char *tag = (*buf) + 1;
243 data1_element *elem = 0;
245 for (; *t && *t != '>' && !isspace(*t); t++);
246 if (*t != '>' && !isspace(*t))
248 logf(LOG_WARN, "d1: %d: Malformed tag", *line);
251 if (isspace(*t)) /* the tag has arguments */
258 for (; *t && *t != '>'; t++);
259 if (*t != '>' && !isspace(*t))
261 logf(LOG_WARN, "d1: %d: Malformed tag", *line);
268 * if end-tag, see if we terminate parent. If so, consume and return.
276 if (!*(tag +1) || (parent->which == DATA1N_root && !strcmp(tag + 1,
277 parent->u.root.type)) ||
278 (parent->which == DATA1N_tag && !strcmp(tag + 1,
291 if (!absyn) /* parent node - what are we? */
293 if (!(absyn = data1_get_absyn(tag)))
295 logf(LOG_WARN, "Unable to acquire abstract syntax for '%s'",
299 res = data1_mk_node(m);
300 res->which = DATA1N_root;
301 res->u.root.type = tag;
302 res->u.root.absyn = absyn;
306 else if (!strncmp(tag, "var", 3))
308 char tclass[DATA1_MAX_SYMBOL], type[DATA1_MAX_SYMBOL];
313 if (sscanf(args, "%s %s %n", tclass, type, &val_offset) != 2)
315 logf(LOG_WARN, "Malformed variant triple at '%s'", tag);
318 if (!(tp = data1_getvartypebyct(parent->root->u.root.absyn->varset,
323 * If we're the first variant in this group, create a parent var,
324 * and insert it before the current variant.
326 if (parent->which != DATA1N_variant)
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;
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.
344 for (p = parent; p->which == DATA1N_variant; p = p->parent)
345 if (p->u.variant.type == tp)
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;
359 else /* tag.. acquire our element in the abstract syntax */
361 data1_node *partag = get_parent_tag(parent);
362 data1_element *e = 0;
365 if (parent->which == DATA1N_variant)
371 if (!(e = partag->u.tag.element))
372 localtag = 1; /* our parent is a local tag */
375 if (!localtag && !(elem = data1_getelementbytagname(absyn,
376 e, tag)) && (data1_gettagbyname(absyn->tagset, tag)))
378 if (parent->which == DATA1N_root)
379 logf(LOG_WARN, "Tag '%s' used out of context", tag);
384 elem = data1_getelementbytagname(absyn, e, tag);
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;
398 res->parent = parent;
399 res->num_children = 0;
405 while ((*pp = data1_read_node(buf, res, line, absyn, m)))
407 res->last_child = *pp;
412 else /* != '<'... this is a body of text */
415 char *data = *buf, *pp = *buf;
417 data1_node *partag = get_parent_tag(parent);
420 if (!parent) /* abort if abstract syntax is undefined */
422 /* Determine length and remove newlines/extra blanks */
423 while (**buf && **buf != '<')
431 while (isspace(**buf))
435 *(pp++) = *((*buf)++);
438 while (isspace(data[len-1]))
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;
453 * Read a record in the native syntax.
455 data1_node *data1_read_record(int (*rf)(void *, char *, size_t), void *fh,
458 static char *buf = 0;
464 if (!buf && !(buf = xmalloc(size = 4096)))
469 if (rd + 4096 > size && !(buf =xrealloc(buf, size *= 2)))
471 if ((res = (*rf)(fh, buf + rd, 4096)) <= 0)
476 return data1_read_node(&bp, 0, &line, 0, m);