Bug fix. Bug introduced by previous commit.
[yaz-moved-to-github.git] / retrieval / d1_attset.c
1 /*
2  * Copyright (c) 1995-1998, Index Data.
3  * See the file LICENSE for details.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: d1_attset.c,v $
7  * Revision 1.11  1998-10-14 13:31:56  adam
8  * Bug fix. Bug introduced by previous commit.
9  *
10  * Revision 1.10  1998/10/13 16:09:48  adam
11  * Added support for arbitrary OID's for tagsets, schemas and attribute sets.
12  * Added support for multiple attribute set references and tagset references
13  * from an abstract syntax file.
14  * Fixed many bad logs-calls in routines that read the various
15  * specifications regarding data1 (*.abs,*.att,...) and made the messages
16  * consistent whenever possible.
17  * Added extra 'lineno' argument to function readconf_line.
18  *
19  * Revision 1.9  1998/05/18 13:07:03  adam
20  * Changed the way attribute sets are handled by the retriaval module.
21  * Extended Explain conversion / schema.
22  * Modified server and client to work with ASN.1 compiled protocol handlers.
23  *
24  * Revision 1.8  1998/02/11 11:53:35  adam
25  * Changed code so that it compiles as C++.
26  *
27  * Revision 1.7  1997/09/17 12:10:34  adam
28  * YAZ version 1.4.
29  *
30  * Revision 1.6  1997/09/05 09:50:56  adam
31  * Removed global data1_tabpath - uses data1_get_tabpath() instead.
32  *
33  * Revision 1.5  1996/05/09 07:27:43  quinn
34  * Multiple local attributes values supported.
35  *
36  * Revision 1.4  1996/02/21  15:23:36  quinn
37  * Reversed fclose and return;
38  *
39  * Revision 1.3  1995/12/13  17:14:26  quinn
40  * *** empty log message ***
41  *
42  * Revision 1.2  1995/11/01  16:34:55  quinn
43  * Making data1 look for tables in data1_tabpath
44  *
45  * Revision 1.1  1995/11/01  11:56:07  quinn
46  * Added Retrieval (data management) functions en masse.
47  *
48  *
49  */
50
51 #include <ctype.h>
52 #include <stdio.h>
53 #include <assert.h>
54 #include <stdlib.h>
55
56 #include <log.h>
57 #include <d1_attset.h>
58 #include <data1.h>
59
60 data1_att *data1_getattbyname(data1_handle dh, data1_attset *s, char *name)
61 {
62     data1_att *r;
63     data1_attset_child *c;
64     
65     /* scan local set */
66     for (r = s->atts; r; r = r->next)
67         if (!data1_matchstr(r->name, name))
68             return r;
69     for (c = s->children; c; c = c->next)
70     {
71         assert (c->child);
72         /* scan included sets */
73         if ((r = data1_getattbyname (dh, c->child, name)))
74             return r;
75     }
76     return 0;
77 }
78
79 data1_attset *data1_empty_attset(data1_handle dh)
80 {
81     NMEM mem = data1_nmem_get (dh);
82     data1_attset *res = (data1_attset*) nmem_malloc(mem,sizeof(*res));
83
84     res->name = 0;
85     res->reference = VAL_NONE;
86     res->atts = 0;
87     res->children = 0;
88     res->next = 0;
89     return res;
90 }
91
92 data1_attset *data1_read_attset(data1_handle dh, const char *file)
93 {
94     data1_attset *res = 0;
95     data1_attset_child **childp;
96     data1_att **attp;
97     FILE *f;
98     NMEM mem = data1_nmem_get (dh);
99     int lineno = 0;
100     int argc;
101     char *argv[50], line[512];
102
103     if (!(f = yaz_path_fopen(data1_get_tabpath(dh), file, "r")))
104         return NULL;
105     res = data1_empty_attset (dh);
106
107     childp = &res->children;
108     attp = &res->atts;
109
110     while ((argc = readconf_line(f, &lineno, line, 512, argv, 50)))
111     {
112         char *cmd = argv[0];
113         if (!strcmp(cmd, "att"))
114         {
115             int num;
116             char *name;
117             data1_att *t;
118             data1_local_attribute *locals;
119             
120             if (argc < 3)
121             {
122                 logf(LOG_WARN, "%s:%d: Bad # of args to att", file, lineno);
123                 continue;
124             }
125             num = atoi (argv[1]);
126             name = argv[2];
127             
128             if (argc == 3) /* no local attributes given */
129             {
130                 locals = (data1_local_attribute *)
131                     nmem_malloc(mem, sizeof(*locals));
132                 locals->local = num;
133                 locals->next = 0;
134             }
135             else /* parse the string "local{,local}" */
136             {
137                 char *p = argv[3];
138                 data1_local_attribute **ap = &locals;
139                 do
140                 {
141                     *ap = (data1_local_attribute *)
142                         nmem_malloc(mem, sizeof(**ap));
143                     (*ap)->local = atoi(p);
144                     (*ap)->next = 0;
145                     ap = &(*ap)->next;
146                 }
147                 while ((p = strchr(p, ',')) && *(++p));
148             }
149             t = *attp = (data1_att *)nmem_malloc(mem, sizeof(*t));
150             t->parent = res;
151             t->name = nmem_strdup(mem, name);
152             t->value = num;
153             t->locals = locals;
154             t->next = 0;
155             attp = &t->next;
156         }
157         else if (!strcmp(cmd, "name"))
158         {
159             if (argc != 2)
160             {
161                 logf(LOG_WARN, "%s:%d: Bad # of args to name", file, lineno);
162                 continue;
163             }
164         }
165         else if (!strcmp(cmd, "reference"))
166         {
167             char *name;
168
169             if (argc != 2)
170             {
171                 logf(LOG_WARN, "%s:%d: Bad # of args to reference",
172                      file, lineno);
173                 continue;
174             }
175             name = argv[1];
176             if ((res->reference = oid_getvalbyname(name)) == VAL_NONE)
177             {
178                 logf(LOG_WARN, "%s:%d: Unknown reference oid '%s'",
179                      file, lineno, name);
180                 fclose(f);
181                 return 0;
182             }
183         }
184         else if (!strcmp(cmd, "ordinal"))
185         {
186             logf (LOG_WARN, "%s:%d: Directive ordinal ignored",
187                   file, lineno);
188         }
189         else if (!strcmp(cmd, "include"))
190         {
191             char *name;
192             data1_attset *attset;
193
194             if (argc != 2)
195             {
196                 logf(LOG_WARN, "%s:%d: Bad # of args to include",
197                      file, lineno);
198                 continue;
199             }
200             name = argv[1];
201
202             if (!(attset = data1_get_attset (dh, name)))
203             {
204                 logf(LOG_WARN, "%s:%d: Include of attset %s failed",
205                      file, lineno, name);
206                 continue;
207                 
208             }
209             *childp = (data1_attset_child *)
210                 nmem_malloc (mem, sizeof(**childp));
211             (*childp)->child = attset;
212             (*childp)->next = 0;
213             childp = &(*childp)->next;
214         }
215         else
216         {
217             logf(LOG_WARN, "%s:%d: Unknown directive '%s'",
218                  file, lineno, cmd);
219         }
220     }
221     fclose(f);
222     return res;
223 }