Updated for YAZ 1.7. HTML output tidy up. Added LOC target.
[egate.git] / fml / fmlsym.c
1 /*
2  * Copyright (c) 1995, the EUROPAGATE consortium (see below).
3  *
4  * The EUROPAGATE consortium members are:
5  *
6  *    University College Dublin
7  *    Danmarks Teknologiske Videnscenter
8  *    An Chomhairle Leabharlanna
9  *    Consejo Superior de Investigaciones Cientificas
10  *
11  * Permission to use, copy, modify, distribute, and sell this software and
12  * its documentation, in whole or in part, for any purpose, is hereby granted,
13  * provided that:
14  *
15  * 1. This copyright and permission notice appear in all copies of the
16  * software and its documentation. Notices of copyright or attribution
17  * which appear at the beginning of any file must remain unchanged.
18  *
19  * 2. The names of EUROPAGATE or the project partners may not be used to
20  * endorse or promote products derived from this software without specific
21  * prior written permission.
22  *
23  * 3. Users of this software (implementors and gateway operators) agree to
24  * inform the EUROPAGATE consortium of their use of the software. This
25  * information will be used to evaluate the EUROPAGATE project and the
26  * software, and to plan further developments. The consortium may use
27  * the information in later publications.
28  * 
29  * 4. Users of this software agree to make their best efforts, when
30  * documenting their use of the software, to acknowledge the EUROPAGATE
31  * consortium, and the role played by the software in their work.
32  *
33  * THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT WARRANTY OF ANY KIND,
34  * EXPRESS, IMPLIED, OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
35  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
36  * IN NO EVENT SHALL THE EUROPAGATE CONSORTIUM OR ITS MEMBERS BE LIABLE
37  * FOR ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF
38  * ANY KIND, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA
39  * OR PROFITS, WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND
40  * ON ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
41  * USE OR PERFORMANCE OF THIS SOFTWARE.
42  *
43  */
44 /*
45  * FML interpreter. Europagate, 1995
46  *
47  * $Log: fmlsym.c,v $
48  * Revision 1.6  2001/02/26 14:32:36  adam
49  * Updated for YAZ 1.7. HTML output tidy up. Added LOC target.
50  *
51  * Revision 1.5  1995/05/16 09:39:34  adam
52  * LICENSE.
53  *
54  * Revision 1.4  1995/03/02  08:06:09  adam
55  * Fml function strsub implemented. New test files marc[45].fml.
56  * New test options in fmltest.
57  *
58  * Revision 1.3  1995/02/23  08:32:06  adam
59  * Changed header.
60  *
61  * Revision 1.1.1.1  1995/02/06  13:48:10  adam
62  * First version of the FML interpreter. It's slow and memory isn't
63  * freed properly. In particular, the FML nodes aren't released yet.
64  *
65  */
66 #include <stdio.h>
67 #include <stdlib.h>
68 #include <assert.h>
69 #include <string.h>
70
71 #include "fmlp.h"
72
73 #define SYM_CHUNK 128
74
75 struct fml_sym {
76     struct fml_sym_info info;
77     struct fml_sym *next;
78     int level;
79     char *name;
80     struct fml_sym *level_link;
81 };
82
83 struct fml_sym_tab {
84     int level;
85     int hash;
86     struct fml_sym **array;
87     struct fml_sym *level_link_0;
88     struct fml_sym *level_link_n;
89     struct fml_sym *free_list;
90 };
91
92 static struct fml_sym *sym_alloc (struct fml_sym_tab *tab) 
93 {
94     struct fml_sym *p = tab->free_list;
95     if (!p)
96     {
97         int i;
98
99         tab->free_list = p = malloc (sizeof(*p) * SYM_CHUNK);
100         assert (p);
101         for (i = 0; i<SYM_CHUNK-1; i++)
102             p[i].next = p+i+1;
103         p[i].next = NULL;
104     }
105     tab->free_list = p->next;
106     return p;
107 }
108
109 static void sym_release (struct fml_sym_tab *tab, struct fml_sym *p)
110 {
111     p->next = tab->free_list;
112     tab->free_list = p;
113 }
114
115 struct fml_sym_tab *fml_sym_open (void)
116 {
117     struct fml_sym_tab *tab;
118     int i;
119
120     tab = malloc (sizeof (*tab));
121     if (!tab)
122         return NULL;
123     tab->level = 1;
124     tab->level_link_0 = NULL;
125     tab->level_link_n = NULL;
126     tab->free_list = NULL;
127     tab->hash = 41;
128     tab->array = malloc (sizeof(*tab->array) * tab->hash);
129     if (!tab->array)
130     {
131         free (tab);
132         return NULL;
133     }
134     for (i = 0; i<tab->hash; i++)
135         tab->array[i] = NULL;
136     return tab;
137 }
138
139 void fml_sym_close (struct fml_sym_tab **tabp)
140 {
141     struct fml_sym *sym, *sym1;
142     int i;
143     for (i = (*tabp)->hash; --i >= 0; )
144         for (sym = (*tabp)->array[i]; sym; sym = sym1)
145         {
146             sym1 = sym->next;
147             free (sym->name);
148             free (sym);
149         }
150     free (*tabp);
151     *tabp = NULL;
152 }
153
154 void fml_sym_push (struct fml_sym_tab *tab)
155 {
156     tab->level ++;
157 }
158
159 void fml_sym_pop (struct fml_sym_tab *tab, void (*ph)(struct fml_sym_info *i))
160 {
161     struct fml_sym **fsp;
162     int i;
163
164     assert (tab->level > 0);
165     for (i = tab->hash; --i >= 0; )
166     {
167         fsp = tab->array + i;
168         while (*fsp)
169         {
170             if ((*fsp)->level == tab->level)
171             {
172                 struct fml_sym *fs;
173
174                 fs = *fsp;
175                 if (ph)
176                     (*ph)(&fs->info);
177                 *fsp = (*fsp)->next;
178                 free (fs->name);
179                 sym_release (tab, fs);
180             }
181             else
182                 fsp = &(*fsp)->next;
183         }
184     }
185     tab->level--;
186 }
187
188 static unsigned fml_sym_hash (const char *s, unsigned hash)
189 {
190     unsigned long v = 0;
191     while (*s)
192         v = v*65599 + *s++;
193     return v % hash;
194 }
195
196
197 static struct fml_sym_info *sym_add (struct fml_sym_tab *tab,
198                                      const char *s, int level)
199 {
200     char *cp;
201     struct fml_sym *sym;
202     struct fml_sym **sym_entry;
203
204     cp = malloc (strlen(s)+1);
205     if (!cp)
206         return NULL;
207     strcpy (cp, s);
208
209     sym = sym_alloc (tab);
210
211     sym_entry = tab->array + fml_sym_hash (s, tab->hash);
212     sym->name = cp;
213     sym->next = *sym_entry;
214     *sym_entry = sym;
215     sym->level = level;
216     if (level)
217     {
218         sym->level_link = tab->level_link_n;
219         tab->level_link_n = sym;
220     }
221     else
222     {
223         sym->level_link = tab->level_link_0;
224         tab->level_link_0 = sym;
225     }
226     return &sym->info;
227 }
228
229 struct fml_sym_info *fml_sym_add (struct fml_sym_tab *tab, const char *s)
230 {
231     return sym_add (tab, s, 0);
232 }
233
234 struct fml_sym_info *fml_sym_add_local (struct fml_sym_tab *tab, const char *s)
235 {
236     return sym_add (tab, s, tab->level);
237 }
238
239 static struct fml_sym_info *sym_lookup (struct fml_sym_tab *tab,
240                                         const char *s, int level)
241 {
242     struct fml_sym *sym;
243     struct fml_sym *sym0 = NULL;
244     struct fml_sym **sym_entry;
245
246     sym_entry = tab->array + fml_sym_hash (s, tab->hash);
247     for (sym = *sym_entry; sym; sym = sym->next)
248         if (!strcmp (sym->name, s))
249             if (!sym0 || sym->level > sym0->level)
250                 sym0 = sym;
251     if (sym0)
252     {
253         assert (sym0->level <= tab->level);
254         if (level && sym0->level != level)
255             return NULL;
256         return &sym0->info;
257     }
258     else
259         return NULL;
260 }
261
262 struct fml_sym_info *fml_sym_lookup (struct fml_sym_tab *tab, const char *s)
263 {
264     return sym_lookup (tab, s, 0);
265 }
266
267 struct fml_sym_info *fml_sym_lookup_local (struct fml_sym_tab *tab,
268                                            const char *s)
269 {
270     return sym_lookup (tab, s, tab->level);
271 }
272