c157ad22b4448e253eefea2f5af11707858a3fea
[yaz-moved-to-github.git] / retrieval / d1_espec.c
1 /*
2  * Copyright (c) 1995, Index Data.
3  * See the file LICENSE for details.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: d1_espec.c,v $
7  * Revision 1.6  1996-07-06 19:58:34  quinn
8  * System headerfiles gathered in yconfig
9  *
10  * Revision 1.5  1996/01/02  08:57:44  quinn
11  * Changed enums in the ASN.1 .h files to #defines. Changed oident.class to oclass
12  *
13  * Revision 1.4  1995/12/05  11:16:10  quinn
14  * Fixed malloc of 0.
15  *
16  * Revision 1.3  1995/11/13  09:27:34  quinn
17  * Fiddling with the variant stuff.
18  *
19  * Revision 1.2  1995/11/01  16:34:56  quinn
20  * Making data1 look for tables in data1_tabpath
21  *
22  * Revision 1.1  1995/11/01  11:56:07  quinn
23  * Added Retrieval (data management) functions en masse.
24  *
25  *
26  */
27
28
29 #include <stdlib.h>
30 #include <assert.h>
31 #include <string.h>
32 #include <ctype.h>
33 #include <xmalloc.h>
34 #include <odr.h>
35 #include <proto.h>
36 #include <log.h>
37 #include <readconf.h>
38 #include <tpath.h>
39 #include <data1.h>
40
41 static Z_Variant *read_variant(int argc, char **argv, ODR o)
42 {
43     Z_Variant *r = odr_malloc(o, sizeof(*r));
44     oident var1;
45     int i;
46
47     var1.proto = PROTO_Z3950;
48     var1.oclass = CLASS_VARSET;
49     var1.value = VAL_VAR1;
50     r->globalVariantSetId = odr_oiddup(o, oid_getoidbyent(&var1));
51
52     if (argc)
53         r->triples = odr_malloc(o, sizeof(Z_Triple*) * argc);
54     else
55         r->triples = 0;
56     r->num_triples = argc;
57     for (i = 0; i < argc; i++)
58     {
59         int class, type;
60         char value[512];
61         Z_Triple *t;
62
63         if (sscanf(argv[i], "(%d,%d,%[^)])", &class, &type, value) < 3)
64         {
65             logf(LOG_WARN, "Syntax error in variant component '%s'",
66                 argv[i]);
67             return 0;
68         }
69         t = r->triples[i] = odr_malloc(o, sizeof(Z_Triple));
70         t->variantSetId = 0;
71         t->class = odr_malloc(o, sizeof(int));
72         *t->class = class;
73         t->type = odr_malloc(o, sizeof(int));
74         *t->type = type;
75         /*
76          * This is wrong.. we gotta look up the correct type for the
77          * variant, I guess... damn this stuff.
78          */
79         if (*value == '@')
80         {
81             t->which = Z_Triple_null;
82             t->value.null = ODR_NULLVAL;
83         }
84         else if (isdigit(*value))
85         {
86             t->which = Z_Triple_integer;
87             t->value.integer = odr_malloc(o, sizeof(*t->value.integer));
88             *t->value.integer = atoi(value);
89         }
90         else
91         {
92             t->which = Z_Triple_internationalString;
93             t->value.internationalString = odr_malloc(o, strlen(value)+1);
94             strcpy(t->value.internationalString, value);
95         }
96     }
97     return r;
98 }
99
100 static Z_Occurrences *read_occurrences(char *occ, ODR o)
101 {
102     Z_Occurrences *op = odr_malloc(o, sizeof(*op));
103     char *p;
104
105     if (!occ)
106     {
107         op->which = Z_Occurrences_values;
108         op->u.values = odr_malloc(o, sizeof(Z_OccurValues));
109         op->u.values->start = odr_malloc(o, sizeof(int));
110         *op->u.values->start = 1;
111         op->u.values->howMany = 0;
112     }
113     else if (!strcmp(occ, "all"))
114     {
115         op->which = Z_Occurrences_all;
116         op->u.all = ODR_NULLVAL;
117     }
118     else if (!strcmp(occ, "last"))
119     {
120         op->which = Z_Occurrences_last;
121         op->u.all = ODR_NULLVAL;
122     }
123     else
124     {
125         Z_OccurValues *ov = odr_malloc(o, sizeof(*ov));
126
127         if (!isdigit(*occ))
128         {
129             logf(LOG_WARN, "Bad occurrences-spec in %s", occ);
130             return 0;
131         }
132         op->which = Z_Occurrences_values;
133         op->u.values = ov;
134         ov->start = odr_malloc(o, sizeof(*ov->start));
135         *ov->start = atoi(occ);
136         if ((p = strchr(occ, '+')))
137         {
138             ov->howMany = odr_malloc(o, sizeof(*ov->howMany));
139             *ov->howMany = atoi(p + 1);
140         }
141         else
142             ov->howMany = 0;
143     }
144     return op;
145 }
146
147
148 static Z_ETagUnit *read_tagunit(char *buf, ODR o)
149 {
150     Z_ETagUnit *u = odr_malloc(o, sizeof(*u));
151     int terms;
152     int type;
153     char value[512], occ[512];
154
155     if (*buf == '*')
156     {
157         u->which = Z_ETagUnit_wildPath;
158         u->u.wildPath = ODR_NULLVAL;
159     }
160     else if (*buf == '?')
161     {
162         u->which = Z_ETagUnit_wildThing;
163         if (buf[1] == ':')
164             u->u.wildThing = read_occurrences(buf+2, o);
165         else
166             u->u.wildThing = read_occurrences(0, o);
167     }
168     else if ((terms = sscanf(buf, "(%d,%[^)]):%[a-z0-9+]", &type, value,
169         occ)) >= 2)
170     {
171         int numval;
172         Z_SpecificTag *t;
173         char *valp = value;
174         int force_string = 0;
175
176         if (*valp == '\'')
177         {
178             valp++;
179             force_string = 1;
180         }
181         u->which = Z_ETagUnit_specificTag;
182         u->u.specificTag = t = odr_malloc(o, sizeof(*t));
183         t->tagType = odr_malloc(o, sizeof(*t->tagType));
184         *t->tagType = type;
185         t->tagValue = odr_malloc(o, sizeof(*t->tagValue));
186         if (!force_string && (numval = atoi(valp)))
187         {
188             t->tagValue->which = Z_StringOrNumeric_numeric;
189             t->tagValue->u.numeric = odr_malloc(o, sizeof(int));
190             *t->tagValue->u.numeric = numval;
191         }
192         else
193         {
194             t->tagValue->which = Z_StringOrNumeric_string;
195             t->tagValue->u.string = odr_malloc(o, strlen(valp)+1);
196             strcpy(t->tagValue->u.string, valp);
197         }
198         if (terms > 2) /* an occurrences-spec exists */
199             t->occurrences = read_occurrences(occ, o);
200         else
201             t->occurrences = 0;
202     }
203     return u;
204 }
205
206 /*
207  * Read an element-set specification from a file.
208  * NOTE: If !o, memory is allocated directly from the heap by odr_malloc().
209  */
210 Z_Espec1 *data1_read_espec1(char *file, ODR o)
211 {
212     FILE *f;
213     int argc, size_esn = 0;
214     char *argv[50], line[512];
215     Z_Espec1 *res = odr_malloc(o, sizeof(*res));
216
217     if (!(f = yaz_path_fopen(data1_tabpath, file, "r")))
218     {
219         logf(LOG_WARN|LOG_ERRNO, "%s", file);
220         return 0;
221     }
222
223     res->num_elementSetNames = 0;
224     res->elementSetNames = 0;
225     res->defaultVariantSetId = 0;
226     res->defaultVariantRequest = 0;
227     res->defaultTagType = 0;
228     res->num_elements = 0;
229     res->elements = 0;
230
231     while ((argc = readconf_line(f, line, 512, argv, 50)))
232         if (!strcmp(argv[0], "elementsetnames"))
233         {
234             int nnames = argc-1, i;
235
236             if (!nnames)
237             {
238                 logf(LOG_WARN, "%s: Empty elementsetnames directive",
239                     file);
240                 continue;
241             }
242
243             res->elementSetNames = odr_malloc(o, sizeof(char*)*nnames);
244             for (i = 0; i < nnames; i++)
245             {
246                 res->elementSetNames[i] = odr_malloc(o, strlen(argv[i+1])+1);
247                 strcpy(res->elementSetNames[i], argv[i+1]);
248             }
249             res->num_elementSetNames = nnames;
250         }
251         else if (!strcmp(argv[0], "defaultvariantsetid"))
252         {
253             if (argc != 2 || !(res->defaultVariantSetId =
254                 odr_getoidbystr(o, argv[1])))
255             {
256                 logf(LOG_WARN, "%s: Bad defaultvariantsetid directive", file);
257                 continue;
258             }
259         }
260         else if (!strcmp(argv[0], "defaulttagtype"))
261         {
262             if (argc != 2)
263             {
264                 logf(LOG_WARN, "%s: Bad defaulttagtype directive", file);
265                 continue;
266             }
267             res->defaultTagType = odr_malloc(o, sizeof(int));
268             *res->defaultTagType = atoi(argv[1]);
269         }
270         else if (!strcmp(argv[0], "defaultvariantrequest"))
271         {
272             if (!(res->defaultVariantRequest = read_variant(argc-1, argv+1, o)))
273             {
274                 logf(LOG_WARN, "%s: Bad defaultvariantrequest", file);
275                 continue;
276             }
277         }
278         else if (!strcmp(argv[0], "simpleelement"))
279         {
280             Z_ElementRequest *er;
281             Z_SimpleElement *se;
282             Z_ETagPath *tp;
283             char *path = argv[1];
284             char *ep;
285             int num, i = 0;
286
287             if (!res->elements)
288                 res->elements = odr_malloc(o, size_esn = 24*sizeof(*er));
289             else if (res->num_elements >= size_esn)
290             {
291                 size_esn *= 2;
292                 res->elements = o ? odr_malloc(o, size_esn) :
293                     xrealloc(res->elements, size_esn);
294             }
295             if (argc < 2)
296             {
297                 logf(LOG_WARN, "%s: Empty simpleelement directive", file);
298                 continue;
299             }
300
301             res->elements[res->num_elements++] = er =
302                 odr_malloc(o, sizeof(*er));
303             er->which = Z_ERequest_simpleElement;
304             er->u.simpleElement = se = odr_malloc(o, sizeof(*se));
305             se->variantRequest = 0;
306             se->path = tp = odr_malloc(o, sizeof(*tp));
307             tp->num_tags = 0;
308             /*
309              * Parse the element selector.
310              */
311             for (num = 1, ep = path; (ep = strchr(ep, '/')); num++, ep++);
312             tp->tags = odr_malloc(o, sizeof(Z_ETagUnit*)*num);
313
314             for ((ep = strchr(path, '/')) ; path ; (void)((path = ep) &&
315                 (ep = strchr(path, '/'))))
316             {
317                 if (ep)
318                     ep++;
319
320                 assert(i<num);
321                 tp->tags[tp->num_tags++] = read_tagunit(path, o);
322             }
323
324             if (argc > 2 && !strcmp(argv[2], "variant"))
325                 se->variantRequest= read_variant(argc-3, argv+3, o);
326         }
327         else
328         {
329             logf(LOG_WARN, "%s: Unknown directive %s", file, argv[0]);
330             fclose(f);
331             return 0;
332         }
333
334     return res;
335 }