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