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