Fixed bug in inetd code. The server listened on tcp:@:9999 even
[yaz-moved-to-github.git] / retrieval / d1_tagset.c
1 /*
2  * Copyright (c) 1995-1997, Index Data.
3  * See the file LICENSE for details.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: d1_tagset.c,v $
7  * Revision 1.6  1997-09-17 12:10:38  adam
8  * YAZ version 1.4.
9  *
10  * Revision 1.5  1997/09/05 09:50:57  adam
11  * Removed global data1_tabpath - uses data1_get_tabpath() instead.
12  *
13  * Revision 1.4  1995/11/13 09:27:38  quinn
14  * Fiddling with the variant stuff.
15  *
16  * Revision 1.3  1995/11/01  16:34:58  quinn
17  * Making data1 look for tables in data1_tabpath
18  *
19  * Revision 1.2  1995/11/01  13:54:49  quinn
20  * Minor adjustments
21  *
22  * Revision 1.1  1995/11/01  11:56:09  quinn
23  * Added Retrieval (data management) functions en masse.
24  *
25  *
26  */
27
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <ctype.h>
31 #include <string.h>
32
33 #include <log.h>
34 #include <tpath.h>
35
36 #include <data1.h>
37
38 /*
39  * We'll probably want to add some sort of hashed index to these lookup-
40  * functions eventually.
41  */
42
43 data1_datatype data1_maptype (data1_handle dh, char *t)
44 {
45     static struct
46     {
47         char *tname;
48         data1_datatype type;
49     } types[] =
50     {
51         {"structured", DATA1K_structured},
52         {"string", DATA1K_string},
53         {"numeric", DATA1K_numeric},
54         {"oid", DATA1K_oid},
55         {"bool", DATA1K_bool},
56         {"generalizedtime", DATA1K_generalizedtime},
57         {"intunit", DATA1K_intunit},
58         {"int", DATA1K_int},
59         {"octetstring", DATA1K_octetstring},
60         {"null", DATA1K_null},
61         {0, -1}
62     };
63     int i;
64
65     for (i = 0; types[i].tname; i++)
66         if (!data1_matchstr(types[i].tname, t))
67             return types[i].type;
68     return 0;
69 }
70
71 data1_tag *data1_gettagbynum (data1_handle dh, data1_tagset *s,
72                               int type, int value)
73 {
74     data1_tag *r;
75
76     for (; s; s = s->next)
77     {
78         /* scan local set */
79         if (type == s->type)
80             for (r = s->tags; r; r = r->next)
81                 if (r->which == DATA1T_numeric && r->value.numeric == value)
82                     return r;
83         /* scan included sets */
84         if (s->children && (r = data1_gettagbynum (dh, s->children,
85                                                    type, value)))
86             return r;
87     }
88     return 0;
89 }
90
91 data1_tag *data1_gettagbyname (data1_handle dh, data1_tagset *s, char *name)
92 {
93     data1_tag *r;
94
95     for (; s; s = s->next)
96     {
97         /* scan local set */
98         for (r = s->tags; r; r = r->next)
99         {
100             data1_name *np;
101
102             for (np = r->names; np; np = np->next)
103                 if (!data1_matchstr(np->name, name))
104                     return r;
105         }
106         /* scan included sets */
107         if (s->children && (r = data1_gettagbyname (dh, s->children, name)))
108             return r;
109     }
110     return 0;
111 }
112
113 data1_tagset *data1_read_tagset (data1_handle dh, char *file)
114 {
115     NMEM mem = data1_nmem_get (dh);
116     char line[512], *r, cmd[512], args[512];
117     data1_tagset *res = 0, **childp;
118     data1_tag **tagp;
119     FILE *f;
120
121     if (!(f = yaz_path_fopen(data1_get_tabpath(dh), file, "r")))
122     {
123         logf(LOG_WARN|LOG_ERRNO, "%s", file);
124         return 0;
125     }
126
127     res = nmem_malloc(mem, sizeof(*res));
128     res->name = 0;
129     res->type = 0;
130     res->tags = 0;
131     res->children = 0;
132     res->next = 0;
133     childp = &res->children;
134     tagp = &res->tags;
135
136     for (;;)
137     {
138         while ((r = fgets(line, 512, f)))
139         {
140             while (*r && isspace(*r))
141                 r++;
142             if (*r && *r != '#')
143                 break;
144         }
145         if (!r)
146         {
147             fclose(f);
148             return res;
149         }
150         if (sscanf(r, "%s %[^\n]", cmd, args) < 2)
151             *args = '\0';
152         if (!strcmp(cmd, "tag"))
153         {
154             int value;
155             char names[512], type[512], *nm;
156             data1_tag *rr;
157             data1_name **npp;
158
159             if (sscanf(args, "%d %s %s", &value, names, type) < 3)
160             {
161                 logf(LOG_WARN, "Bad number of parms in '%s' in %s",
162                     args, file);
163                 fclose(f);
164                 return 0;
165             }
166             rr = *tagp = nmem_malloc(mem, sizeof(*rr));
167             rr->tagset = res;
168             rr->next = 0;
169             rr->which = DATA1T_numeric;
170             rr->value.numeric = value;
171             /*
172              * how to deal with local numeric tags?
173              */
174
175             if (!(rr->kind = data1_maptype(dh, type)))
176             {
177                 logf(LOG_WARN, "Unknown datatype %s in %s", type, file);
178                 fclose(f);
179                 return 0;
180             }
181             
182             /* read namelist */
183             nm = names;
184             npp = &rr->names;
185             do
186             {
187                 char *e;
188
189                 *npp = nmem_malloc(mem, sizeof(**npp));
190                 if ((e = strchr(nm, '/')))
191                     *(e++) = '\0';
192                 (*npp)->name = nmem_strdup(mem, nm);
193                 (*npp)->next = 0;
194                 npp = &(*npp)->next;
195                 nm = e;
196             }
197             while (nm);
198             tagp = &rr->next;
199         }
200         else if (!strcmp(cmd, "name"))
201         {
202             char name[512];
203
204             if (!sscanf(args, "%s", name))
205             {
206                 logf(LOG_WARN, "%s malformed name directive in %s", file);
207                 fclose(f);
208                 return 0;
209             }
210             res->name = nmem_strdup(mem, args);
211         }
212         else if (!strcmp(cmd, "reference"))
213         {
214             char name[512];
215
216             if (!sscanf(args, "%s", name))
217             {
218                 logf(LOG_WARN, "%s malformed reference directive in %s", file);
219                 fclose(f);
220                 return 0;
221             }
222             if ((res->reference = oid_getvalbyname(name)) == VAL_NONE)
223             {
224                 logf(LOG_WARN, "Unknown tagset ref '%s' in %s", name, file);
225                 fclose(f);
226                 return 0;
227             }
228         }
229         else if (!strcmp(cmd, "type"))
230         {
231             if (!sscanf(args, "%d", &res->type))
232             {
233                 logf(LOG_WARN, "%s malformed type directive in %s", file);
234                 fclose(f);
235                 return 0;
236             }
237         }
238         else if (!strcmp(cmd, "include"))
239         {
240             char name[512];
241
242             if (!sscanf(args, "%s", name))
243             {
244                 logf(LOG_WARN, "%s malformed reference directive in %s", file);
245                 fclose(f);
246                 return 0;
247             }
248             if (!(*childp = data1_read_tagset (dh, name)))
249             {
250                 logf(LOG_WARN, "Inclusion failed in %s", file);
251                 fclose(f);
252                 return 0;
253             }
254             childp = &(*childp)->next;
255         }
256         else
257         {
258             logf(LOG_WARN, "Unknown directive '%s' in %s", cmd, file);
259             fclose(f);
260             return 0;
261         }
262     }
263 }