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