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.12 1995/05/16 09:39:34 adam
51 * Revision 1.11 1995/03/02 10:18:48 adam
54 * Revision 1.10 1995/03/02 08:06:03 adam
55 * Fml function strsub implemented. New test files marc[45].fml.
56 * New test options in fmltest.
58 * Revision 1.9 1995/02/27 09:01:20 adam
59 * Regular expression support. Argument passing by name option. New FML
62 * Revision 1.8 1995/02/23 08:32:05 adam
65 * Revision 1.6 1995/02/10 18:15:52 adam
66 * FML function 'strcmp' implemented. This function can be used to
67 * test for existence of MARC fields.
69 * Revision 1.5 1995/02/09 14:37:18 adam
70 * Removed .depend from cvs. Removed function fml_mk_list.
72 * Revision 1.4 1995/02/09 14:33:37 adam
73 * Split source fml.c and define relevant build-in functions in separate
74 * files. New operators mult, div, not, llen implemented.
76 * Revision 1.3 1995/02/09 13:07:15 adam
77 * Nodes are freed now. Many bugs fixed.
79 * Revision 1.2 1995/02/06 15:23:26 adam
80 * Added some more relational operators (le,ne,ge). Added increment
81 * and decrement operators. Function index changed, so that first
82 * element is 1 - not 0. Function fml_atom_val edited.
84 * Revision 1.1.1.1 1995/02/06 13:48:10 adam
85 * First version of the FML interpreter. It's slow and memory isn't
86 * freed properly. In particular, the FML nodes aren't released yet.
96 #define FML_ATOM_CHUNK 1024
97 #define FML_NODE_CHUNK 1024
99 static int no_nodes = 0;
100 static int no_atoms = 0;
102 struct fml_node *fml_node_alloc (Fml fml)
106 if (! fml->node_free_list)
110 n = fml->node_free_list = malloc (sizeof(*n) * FML_NODE_CHUNK);
113 (*fml->err_handle)(FML_ERR_NOMEM);
116 for (i = FML_ATOM_CHUNK-1; --i >= 0; n++)
120 n = fml->node_free_list;
121 fml->node_free_list = n->p[1];
122 n->p[0] = n->p[1] = NULL;
128 static struct fml_atom *atom_malloc (Fml fml)
132 if (! fml->atom_free_list)
136 fa = fml->atom_free_list = malloc (sizeof(*fa) * FML_ATOM_CHUNK);
139 (*fml->err_handle)(FML_ERR_NOMEM);
142 for (i = FML_ATOM_CHUNK-1; --i >= 0; fa++)
146 fa = fml->atom_free_list;
147 fml->atom_free_list = fa->next;
152 static void atom_delete (Fml fml, struct fml_atom *a)
154 a->next = fml->atom_free_list;
155 fml->atom_free_list = a;
159 static struct fml_atom *atom_copy (Fml fml, struct fml_atom *a)
161 struct fml_atom *a0, *a1;
163 a0 = a1 = atom_malloc (fml);
166 memcpy (&a1->buf, &a->buf, FML_ATOM_BUF);
170 a1 = a1->next = atom_malloc (fml);
176 struct fml_atom *fml_atom_alloc (Fml fml, char *str)
179 struct fml_atom *a, *a0;
181 a0 = a = atom_malloc (fml);
182 strncpy (a->buf, str, FML_ATOM_BUF);
183 while (strlen (str+soff) >= FML_ATOM_BUF)
187 an = atom_malloc (fml);
189 soff += FML_ATOM_BUF;
190 strncpy (an->buf, str+soff, FML_ATOM_BUF);
197 int fml_atom_cmp (Fml fml, struct fml_atom *a1, struct fml_atom *a2)
202 n = strncmp (a1->buf, a2->buf, FML_ATOM_BUF);
215 int fml_atom_str (struct fml_atom *a, char *str)
223 memcpy (str+len, a->buf, FML_ATOM_BUF);
227 for (i=0; i<FML_ATOM_BUF && a->buf[i]; i++)
231 memcpy (str+len, a->buf, i);
238 void fml_atom_strx (struct fml_atom *a, char *str, int max)
243 while (a->next && len < max - 2*FML_ATOM_BUF)
245 memcpy (str+len, a->buf, FML_ATOM_BUF);
249 strncpy (str+len, a->buf, FML_ATOM_BUF-1);
250 str[len+FML_ATOM_BUF-1] = '\0';
253 int fml_atom_len (struct fml_atom *a)
263 len += strlen (a->buf);
268 int fml_atom_val (struct fml_atom *a)
270 static char arg[256];
273 return atoi (a->buf);
274 fml_atom_strx (a, arg, 200);
278 struct fml_node *fml_mk_node_val (Fml fml, int val)
283 sprintf (arg, "%d", val);
284 fn = fml_node_alloc (fml);
286 fn->p[0] = fml_atom_alloc (fml, arg);
290 void fml_node_delete (Fml fml, struct fml_node *fn)
296 atom_delete (fml, fn->p[0]);
298 fml_node_delete (fml, fn->p[0]);
301 fn->p[1] = fml->node_free_list;
302 fml->node_free_list = fn;
309 struct fml_node *fml_node_copy (Fml fml, struct fml_node *fn)
311 struct fml_node *fn0, *fn1;
315 fn1 = fn0 = fml_node_alloc (fml);
321 fn1->p[0] = atom_copy (fml, fn->p[0]);
324 fn1->p[0] = fml_node_copy (fml, fn->p[0]);
328 fn1 = fn1->p[1] = fml_node_alloc (fml);
333 void fml_node_stat (Fml fml)
336 printf ("<<node=%d, atom=%d>>", no_nodes, no_atoms);
339 struct fml_atom *fml_atom_strsub (Fml fml, struct fml_atom *a, int o, int l)
341 static char buf[512];
344 int ol = fml_atom_len (a);
348 cp = malloc (ol + 1);
353 fml_atom_str (a, buf);
356 an = fml_atom_alloc (fml, buf+o);