Updated for YAZ 1.7. HTML output tidy up. Added LOC target.
[egate.git] / fml / fmlstr.c
index 440dabb..3ae3b9e 100644 (file)
@@ -1,8 +1,71 @@
 /*
+ * Copyright (c) 1995, the EUROPAGATE consortium (see below).
+ *
+ * The EUROPAGATE consortium members are:
+ *
+ *    University College Dublin
+ *    Danmarks Teknologiske Videnscenter
+ *    An Chomhairle Leabharlanna
+ *    Consejo Superior de Investigaciones Cientificas
+ *
+ * Permission to use, copy, modify, distribute, and sell this software and
+ * its documentation, in whole or in part, for any purpose, is hereby granted,
+ * provided that:
+ *
+ * 1. This copyright and permission notice appear in all copies of the
+ * software and its documentation. Notices of copyright or attribution
+ * which appear at the beginning of any file must remain unchanged.
+ *
+ * 2. The names of EUROPAGATE or the project partners may not be used to
+ * endorse or promote products derived from this software without specific
+ * prior written permission.
+ *
+ * 3. Users of this software (implementors and gateway operators) agree to
+ * inform the EUROPAGATE consortium of their use of the software. This
+ * information will be used to evaluate the EUROPAGATE project and the
+ * software, and to plan further developments. The consortium may use
+ * the information in later publications.
+ * 
+ * 4. Users of this software agree to make their best efforts, when
+ * documenting their use of the software, to acknowledge the EUROPAGATE
+ * consortium, and the role played by the software in their work.
+ *
+ * THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS, IMPLIED, OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
+ * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
+ * IN NO EVENT SHALL THE EUROPAGATE CONSORTIUM OR ITS MEMBERS BE LIABLE
+ * FOR ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF
+ * ANY KIND, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA
+ * OR PROFITS, WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND
+ * ON ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
+ * USE OR PERFORMANCE OF THIS SOFTWARE.
+ *
+ */
+/*
  * FML interpreter. Europagate, 1995
  *
  * $Log: fmlstr.c,v $
- * Revision 1.1  1995/02/10 18:15:53  adam
+ * Revision 1.8  2001/02/26 14:32:36  adam
+ * Updated for YAZ 1.7. HTML output tidy up. Added LOC target.
+ *
+ * Revision 1.7  1995/05/16 09:39:34  adam
+ * LICENSE.
+ *
+ * Revision 1.6  1995/03/27  12:49:51  adam
+ * Removed CFLAGS def. Bug fix (compile error when USE_GNU_REGEX=0).
+ *
+ * Revision 1.5  1995/03/02  08:06:07  adam
+ * Fml function strsub implemented. New test files marc[45].fml.
+ * New test options in fmltest.
+ *
+ * Revision 1.4  1995/02/27  09:01:21  adam
+ * Regular expression support. Argument passing by name option. New FML
+ * function strlen.
+ *
+ * Revision 1.3  1995/02/23  08:32:06  adam
+ * Changed header.
+ *
+ * Revision 1.1  1995/02/10  18:15:53  adam
  * FML function 'strcmp' implemented. This function can be used to
  * test for existence of MARC fields.
  *
 #include <assert.h>
 #include <stdlib.h>
 #include <stdio.h>
+#include <string.h>
 
 #include "fmlp.h"
 
+#if USE_GNU_REGEX
+#include <regex.h>
+#endif
+
+#if USE_GNU_REGEX
+struct reg_cache {
+    struct re_pattern_buffer buf;
+    char *pattern;
+    struct reg_cache *next;
+};
+
+static int no_in_use = 0;
+static struct reg_cache *reg_cache_list = NULL;
+
+struct reg_cache *fml_reg_compile (const char *pattern)
+{
+    struct reg_cache *list, *last = NULL;
+    for (list = reg_cache_list; list; list = list->next)
+    {
+        if (!strcmp (pattern, list->pattern))
+            return list;
+        last = list;
+    }
+    if (no_in_use >= 20)
+    {
+        for (list = reg_cache_list; list->next->next; list = list->next)
+            ;
+        free (list->next->pattern);
+        regfree (&list->next->buf);
+        free (list->next);
+        list->next = NULL;
+    }
+    else
+        no_in_use++;
+    list = malloc (sizeof (*list));
+    assert (list);
+    list->next = reg_cache_list;
+    reg_cache_list = list;
+    list->pattern = malloc (strlen(pattern)+1);
+    assert (list->pattern);
+    strcpy (list->pattern, pattern);
+
+    re_syntax_options = RE_SYNTAX_GREP;
+    list->buf.translate = NULL;
+    list->buf.fastmap = NULL;
+    list->buf.buffer = NULL;
+    list->buf.allocated = 0;
+    re_compile_pattern (pattern, strlen(pattern), &list->buf);
+    return list;
+}
+
+static int fml_reg_match (struct reg_cache *reg_pat, const char *str)
+{
+    int ret, len = strlen (str);
+
+    ret = re_match (&reg_pat->buf, str, len, 0, NULL);
+    if (ret == len)
+         return 1;
+    return 0;
+}
+
+static struct fml_node *fml_exec_match (Fml fml, struct fml_node **lp, 
+                                         struct token *tp)
+{
+    struct reg_cache *reg;
+    struct fml_node *fn;
+    const char *cp;
+    char pattern[128];
+    char sstring[128];
+
+    fml_cmd_lex (lp, tp); 
+    if (tp->kind == 't')
+    {
+        cp = tp->tokenbuf;
+        fml_cmd_lex (lp, tp);
+    }
+    else
+    {
+        fn = fml_expr_term (fml, lp, tp);
+        if (!fn->is_atom)
+       {
+           fml_node_delete (fml, fn);
+           return NULL;
+        }
+        fml_atom_str (fn->p[0], pattern);
+       fml_node_delete (fml, fn);
+        cp = pattern;
+    }
+    reg = fml_reg_compile (cp);
+    fn = fml_expr_term (fml, lp, tp);
+    if (!fn->is_atom)
+    {
+        fml_node_delete (fml, fn);
+        return NULL;
+    }
+    fml_atom_str (fn->p[0], sstring);
+    fml_node_delete (fml, fn);
+    if (fml_reg_match (reg, sstring))
+       return fml_mk_node_val (fml, 1);
+    return NULL;
+}
+
+#endif
+
+static struct fml_node *fml_exec_strlen (Fml fml, struct fml_node **lp, 
+                                         struct token *tp)
+{
+    struct fml_node *fn;
+    int len = 0;
+
+    fml_cmd_lex (lp, tp);
+    fn = fml_expr_term (fml, lp, tp);
+    while (fn)
+    {
+        if (fn->is_atom)
+            len += fml_atom_len (fn->p[0]);
+        fn = fn->p[1];
+        if (fn)
+            len++;
+    }
+    fml_node_delete (fml, fn);
+    return fml_mk_node_val (fml, len);
+}
+
+static struct fml_node *fml_exec_strsub (Fml fml, struct fml_node **lp,
+                                        struct token *tp)
+{
+    struct fml_node *fn_str;
+    struct fml_node *fn_offset;
+    struct fml_node *fn_length;
+    struct fml_node *fn_res;
+    int offset, length;
+
+    fml_cmd_lex (lp, tp);
+    fn_str = fml_expr_term (fml, lp, tp);
+    fn_offset = fml_expr_term (fml, lp, tp);
+    fn_length = fml_expr_term (fml, lp, tp);
+    if (!fn_offset->is_atom || !fn_length->is_atom || !fn_str->is_atom)
+    {
+        fml_node_delete (fml, fn_str);
+        fml_node_delete (fml, fn_offset);
+        fml_node_delete (fml, fn_length);
+        return NULL;
+    }
+    offset = fml_atom_val (fn_offset->p[0]);
+    fml_node_delete (fml, fn_offset);
+    length = fml_atom_val (fn_length->p[0]);
+    fml_node_delete (fml, fn_length);
+    if (offset == 0 && fml_atom_len (fn_str->p[0]) < length)
+        return fn_str;
+    fn_res = fml_node_alloc (fml);
+    fn_res->is_atom = 1;
+    fn_res->p[0]= fml_atom_strsub (fml, fn_str->p[0], offset, length);
+    fml_node_delete (fml, fn_str);
+    return fn_res;
+}
+
 static struct fml_node *fml_exec_strcmp (Fml fml, struct fml_node **lp, 
                                          struct token *tp)
 {
@@ -34,6 +255,8 @@ static struct fml_node *fml_exec_strcmp (Fml fml, struct fml_node **lp,
         arg = "1";
     else 
         arg = "-1";
+    fml_node_delete (fml, fn1);
+    fml_node_delete (fml, fn2);
     fn = fml_node_alloc (fml);
     fn->is_atom = 1;
     fn->p[0] = fml_atom_alloc (fml, arg);
@@ -47,4 +270,15 @@ void fml_str_init (Fml fml)
     sym_info = fml_sym_add (fml->sym_tab, "strcmp");
     sym_info->kind = FML_CPREFIX;
     sym_info->prefix = fml_exec_strcmp;
+    sym_info = fml_sym_add (fml->sym_tab, "strlen");
+    sym_info->kind = FML_CPREFIX;
+    sym_info->prefix = fml_exec_strlen;
+    sym_info = fml_sym_add (fml->sym_tab, "strsub");
+    sym_info->kind = FML_CPREFIX;
+    sym_info->prefix = fml_exec_strsub;
+#if USE_GNU_REGEX
+    sym_info = fml_sym_add (fml->sym_tab, "match");
+    sym_info->kind = FML_CPREFIX;
+    sym_info->prefix = fml_exec_match;
+#endif
 }