2 * Copyright (c) 1995, the EUROPAGATE consortium (see below).
4 * The EUROPAGATE consortium members are:
6 * University College Dublin
7 * Danmarks Teknologiske Videnscenter
8 * An Chomhairle Leabharlanna
9 * Consejo Superior de Investigaciones Cientificas
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,
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.
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.
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.
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.
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.
45 * FML interpreter. Europagate, 1995
48 * Revision 1.13 1996/01/29 17:52:05 adam
49 * Minor change - removed unnecessary &.
51 * Revision 1.12 1995/05/16 09:39:34 adam
54 * Revision 1.11 1995/03/02 10:18:48 adam
57 * Revision 1.10 1995/03/02 08:06:03 adam
58 * Fml function strsub implemented. New test files marc[45].fml.
59 * New test options in fmltest.
61 * Revision 1.9 1995/02/27 09:01:20 adam
62 * Regular expression support. Argument passing by name option. New FML
65 * Revision 1.8 1995/02/23 08:32:05 adam
68 * Revision 1.6 1995/02/10 18:15:52 adam
69 * FML function 'strcmp' implemented. This function can be used to
70 * test for existence of MARC fields.
72 * Revision 1.5 1995/02/09 14:37:18 adam
73 * Removed .depend from cvs. Removed function fml_mk_list.
75 * Revision 1.4 1995/02/09 14:33:37 adam
76 * Split source fml.c and define relevant build-in functions in separate
77 * files. New operators mult, div, not, llen implemented.
79 * Revision 1.3 1995/02/09 13:07:15 adam
80 * Nodes are freed now. Many bugs fixed.
82 * Revision 1.2 1995/02/06 15:23:26 adam
83 * Added some more relational operators (le,ne,ge). Added increment
84 * and decrement operators. Function index changed, so that first
85 * element is 1 - not 0. Function fml_atom_val edited.
87 * Revision 1.1.1.1 1995/02/06 13:48:10 adam
88 * First version of the FML interpreter. It's slow and memory isn't
89 * freed properly. In particular, the FML nodes aren't released yet.
99 #define FML_ATOM_CHUNK 1024
100 #define FML_NODE_CHUNK 1024
102 static int no_nodes = 0;
103 static int no_atoms = 0;
105 struct fml_node *fml_node_alloc (Fml fml)
109 if (! fml->node_free_list)
113 n = fml->node_free_list = malloc (sizeof(*n) * FML_NODE_CHUNK);
116 (*fml->err_handle)(FML_ERR_NOMEM);
119 for (i = FML_ATOM_CHUNK-1; --i >= 0; n++)
123 n = fml->node_free_list;
124 fml->node_free_list = n->p[1];
125 n->p[0] = n->p[1] = NULL;
131 static struct fml_atom *atom_malloc (Fml fml)
135 if (! fml->atom_free_list)
139 fa = fml->atom_free_list = malloc (sizeof(*fa) * FML_ATOM_CHUNK);
142 (*fml->err_handle)(FML_ERR_NOMEM);
145 for (i = FML_ATOM_CHUNK-1; --i >= 0; fa++)
149 fa = fml->atom_free_list;
150 fml->atom_free_list = fa->next;
155 static void atom_delete (Fml fml, struct fml_atom *a)
157 a->next = fml->atom_free_list;
158 fml->atom_free_list = a;
162 static struct fml_atom *atom_copy (Fml fml, struct fml_atom *a)
164 struct fml_atom *a0, *a1;
166 a0 = a1 = atom_malloc (fml);
169 memcpy (a1->buf, a->buf, FML_ATOM_BUF);
173 a1 = a1->next = atom_malloc (fml);
179 struct fml_atom *fml_atom_alloc (Fml fml, char *str)
182 struct fml_atom *a, *a0;
184 a0 = a = atom_malloc (fml);
185 strncpy (a->buf, str, FML_ATOM_BUF);
186 while (strlen (str+soff) >= FML_ATOM_BUF)
190 an = atom_malloc (fml);
192 soff += FML_ATOM_BUF;
193 strncpy (an->buf, str+soff, FML_ATOM_BUF);
200 int fml_atom_cmp (Fml fml, struct fml_atom *a1, struct fml_atom *a2)
205 n = strncmp (a1->buf, a2->buf, FML_ATOM_BUF);
218 int fml_atom_str (struct fml_atom *a, char *str)
226 memcpy (str+len, a->buf, FML_ATOM_BUF);
230 for (i=0; i<FML_ATOM_BUF && a->buf[i]; i++)
234 memcpy (str+len, a->buf, i);
241 void fml_atom_strx (struct fml_atom *a, char *str, int max)
246 while (a->next && len < max - 2*FML_ATOM_BUF)
248 memcpy (str+len, a->buf, FML_ATOM_BUF);
252 strncpy (str+len, a->buf, FML_ATOM_BUF-1);
253 str[len+FML_ATOM_BUF-1] = '\0';
256 int fml_atom_len (struct fml_atom *a)
266 len += strlen (a->buf);
271 int fml_atom_val (struct fml_atom *a)
273 static char arg[256];
276 return atoi (a->buf);
277 fml_atom_strx (a, arg, 200);
281 struct fml_node *fml_mk_node_val (Fml fml, int val)
286 sprintf (arg, "%d", val);
287 fn = fml_node_alloc (fml);
289 fn->p[0] = fml_atom_alloc (fml, arg);
293 void fml_node_delete (Fml fml, struct fml_node *fn)
299 atom_delete (fml, fn->p[0]);
301 fml_node_delete (fml, fn->p[0]);
304 fn->p[1] = fml->node_free_list;
305 fml->node_free_list = fn;
312 struct fml_node *fml_node_copy (Fml fml, struct fml_node *fn)
314 struct fml_node *fn0, *fn1;
318 fn1 = fn0 = fml_node_alloc (fml);
324 fn1->p[0] = atom_copy (fml, fn->p[0]);
327 fn1->p[0] = fml_node_copy (fml, fn->p[0]);
331 fn1 = fn1->p[1] = fml_node_alloc (fml);
336 void fml_node_stat (Fml fml)
339 printf ("<<node=%d, atom=%d>>", no_nodes, no_atoms);
342 struct fml_atom *fml_atom_strsub (Fml fml, struct fml_atom *a, int o, int l)
344 static char buf[512];
347 int ol = fml_atom_len (a);
351 cp = malloc (ol + 1);
356 fml_atom_str (a, buf);
359 an = fml_atom_alloc (fml, buf+o);