Fixed a bug that caused data1_read_node to core dump when no abstract
[yaz-moved-to-github.git] / retrieval / d1_doespec.c
1 /*
2  * Copyright (c) 1995, Index Data.
3  * See the file LICENSE for details.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: d1_doespec.c,v $
7  * Revision 1.5  1996-07-06 19:58:34  quinn
8  * System headerfiles gathered in yconfig
9  *
10  * Revision 1.4  1996/06/07  11:04:32  quinn
11  * Fixed tag->tagset dependency
12  *
13  * Revision 1.3  1995/11/13  09:27:33  quinn
14  * Fiddling with the variant stuff.
15  *
16  * Revision 1.2  1995/11/01  13:54:45  quinn
17  * Minor adjustments
18  *
19  * Revision 1.1  1995/11/01  11:56:07  quinn
20  * Added Retrieval (data management) functions en masse.
21  *
22  *
23  */
24
25
26 #include <assert.h>
27 #include <oid.h>
28 #include <log.h>
29 #include <proto.h>
30 #include <data1.h>
31
32 static int match_children(data1_node *n, Z_Espec1 *e, int i, Z_ETagUnit **t,
33     int num);
34
35 static int match_children_wildpath(data1_node *n, Z_Espec1 *e, int i,
36     Z_ETagUnit **t, int num)
37 {return 0;}
38
39 /*
40  * Locate a specific triple within a variant.
41  * set is the set to look for, universal set is the set that applies to a
42  * triple with an unknown set.
43  */
44 static Z_Triple *find_triple(Z_Variant *var, oid_value universalset,
45     oid_value set, int class, int type)
46 {
47     int i;
48     oident *defaultsetent = oid_getentbyoid(var->globalVariantSetId);
49     oid_value defaultset = defaultsetent ? defaultsetent->value :
50         universalset;
51
52     for (i = 0; i < var->num_triples; i++)
53     {
54         oident *cursetent =
55             oid_getentbyoid(var->triples[i]->variantSetId);
56         oid_value curset = cursetent ? cursetent->value : defaultset;
57
58         if (set == curset &&
59             *var->triples[i]->class == class &&
60             *var->triples[i]->type == type)
61             return var->triples[i];
62     }
63     return 0;
64 }
65
66 static void mark_subtree(data1_node *n, int make_variantlist, int no_data,
67     int get_bytes, Z_Variant *vreq)
68 {
69     data1_node *c;
70
71     if (n->which == DATA1N_tag && (!n->child || n->child->which != DATA1N_tag))
72     {
73         n->u.tag.node_selected = 1;
74         n->u.tag.make_variantlist = make_variantlist;
75         n->u.tag.no_data_requested = no_data;
76         n->u.tag.get_bytes = get_bytes;
77     }
78
79     for (c = n->child; c; c = c->next)
80     {
81         if (c->which == DATA1N_tag && (!n->child ||
82             n->child->which != DATA1N_tag))
83         {
84             c->u.tag.node_selected = 1;
85             c->u.tag.make_variantlist = make_variantlist;
86             c->u.tag.no_data_requested = no_data;
87             c->u.tag.get_bytes = get_bytes;
88         }
89         mark_subtree(c, make_variantlist, no_data, get_bytes, vreq);
90     }
91 }
92
93 static int match_children_here(data1_node *n, Z_Espec1 *e, int i,
94     Z_ETagUnit **t, int num)
95 {
96     int counter = 0, hits = 0;
97     data1_node *c;
98     Z_ETagUnit *tp = *t;
99     Z_Occurrences *occur;
100
101     for (c = n->child; c ; c = c->next)
102     {
103         data1_tag *tag = 0;
104
105         if (c->which != DATA1N_tag)
106             return 0;
107
108         if (tp->which == Z_ETagUnit_specificTag)
109         {
110             Z_SpecificTag *want = tp->u.specificTag;
111             occur = want->occurrences;
112             if (c->u.tag.element)
113                 tag = c->u.tag.element->tag;
114             if (*want->tagType != ((tag && tag->tagset) ? tag->tagset->type :
115                 3))
116                 continue;
117             if (want->tagValue->which == Z_StringOrNumeric_numeric)
118             {
119                 if (!tag || tag->which != DATA1T_numeric)
120                     continue;
121                 if (*want->tagValue->u.numeric != tag->value.numeric)
122                     continue;
123             }
124             else
125             {
126                 assert(want->tagValue->which == Z_StringOrNumeric_string);
127                 if (tag && tag->which != DATA1T_string)
128                     continue;
129                 if (data1_matchstr(want->tagValue->u.string,
130                     tag ? tag->value.string : c->u.tag.tag))
131                     continue;
132             }
133         }
134         else
135             occur = tp->u.wildThing;
136
137         /*
138          * Ok, so we have a matching tag. Are we within occurrences-range?
139          */
140         counter++;
141         if (occur && occur->which == Z_Occurrences_last)
142         {
143             logf(LOG_WARN, "Can't do occurrences=last (yet)");
144             return 0;
145         }
146         if (!occur || occur->which == Z_Occurrences_all ||
147             (occur->which == Z_Occurrences_values && counter >=
148             *occur->u.values->start))
149         {
150             if (match_children(c, e, i, t + 1, num - 1))
151             {
152                 c->u.tag.node_selected = 1;
153                 /*
154                  * Consider the variant specification if this is a complete
155                  * match.
156                  */
157                 if (num == 1)
158                 {
159                     int show_variantlist = 0;
160                     int no_data = 0;
161                     int get_bytes = -1;
162
163                     Z_Variant *vreq =
164                         e->elements[i]->u.simpleElement->variantRequest;
165                     oident *defset = oid_getentbyoid(e->defaultVariantSetId);
166                     oid_value defsetval = defset ? defset->value : VAL_NONE;
167                     oid_value var1 = oid_getvalbyname("Variant-1");
168
169                     if (!vreq)
170                         vreq = e->defaultVariantRequest;
171
172                     if (vreq)
173                     {
174                         Z_Triple *r;
175
176                         /*
177                          * 6,5: meta-data requested, variant list.
178                          */
179                         if (find_triple(vreq, defsetval, var1, 6, 5))
180                             show_variantlist = 1;
181                         /*
182                          * 9,1: Miscellaneous, no data requested.
183                          */
184                         if (find_triple(vreq, defsetval, var1, 9, 1))
185                             no_data = 1;
186
187                         /* howmuch */
188                         if ((r = find_triple(vreq, defsetval, var1, 5, 5)))
189                             if (r->which == Z_Triple_integer)
190                                 get_bytes = *r->value.integer;
191                     }
192                     mark_subtree(c, show_variantlist, no_data, get_bytes, vreq);
193                 }
194                 hits++;
195                 /*
196                  * have we looked at enough children?
197                  */
198                 if (!occur || (occur->which == Z_Occurrences_values &&
199                     counter - *occur->u.values->start >=
200                     *occur->u.values->howMany - 1))
201                     return hits;
202             }
203         }
204     }
205     return hits;
206 }
207
208 static int match_children(data1_node *n, Z_Espec1 *e, int i, Z_ETagUnit **t,
209     int num)
210 {
211     int res;
212
213     if (!num)
214         return 1;
215     switch (t[0]->which)
216     {
217         case Z_ETagUnit_wildThing:
218         case Z_ETagUnit_specificTag: res = match_children_here(n, e, i,
219             t, num); break;
220         case Z_ETagUnit_wildPath: res = match_children_wildpath(n, e, i,
221             t, num); break;
222         default:
223             abort();
224     }
225     return res;
226 }
227
228 int data1_doespec1(data1_node *n, Z_Espec1 *e)
229 {
230     int i;
231
232     for (i = 0; i < e->num_elements; i++)
233     {
234         if (e->elements[i]->which != Z_ERequest_simpleElement)
235             return 100;
236         match_children(n, e, i, e->elements[i]->u.simpleElement->path->tags,
237             e->elements[i]->u.simpleElement->path->num_tags);
238     }
239     return 0;
240 }