Using the new ylog.h everywhere, and fixing what that breaks!
[idzebra-moved-to-github.git] / recctrl / recctrl.c
index 9530959..c4dbdbc 100644 (file)
-/*
- * Copyright (C) 1994-1999, Index Data
- * All rights reserved.
- * Sebastian Hammer, Adam Dickmeiss
- *
- * $Log: recctrl.c,v $
- * Revision 1.5  1999-05-26 07:49:14  adam
- * C++ compilation.
- *
- * Revision 1.4  1999/05/20 12:57:18  adam
- * Implemented TCL filter. Updated recctrl system.
- *
- * Revision 1.3  1998/10/16 08:14:36  adam
- * Updated record control system.
- *
- * Revision 1.2  1996/10/29 14:03:16  adam
- * Include zebrautl.h instead of alexutil.h.
- *
- * Revision 1.1  1996/10/11 10:57:24  adam
- * New module recctrl. Used to manage records (extract/retrieval).
- *
- * Revision 1.5  1996/06/04 10:18:59  adam
- * Minor changes - removed include of ctype.h.
- *
- * Revision 1.4  1995/12/04  17:59:24  adam
- * More work on regular expression conversion.
- *
- * Revision 1.3  1995/12/04  14:22:30  adam
- * Extra arg to recType_byName.
- * Started work on new regular expression parsed input to
- * structured records.
- *
- * Revision 1.2  1995/11/15  14:46:19  adam
- * Started work on better record management system.
- *
- * Revision 1.1  1995/09/27  12:22:28  adam
- * More work on extract in record control.
- * Field name is not in isam keys but in prefix in dictionary words.
- *
- */
+/* $Id: recctrl.c,v 1.13 2004-11-19 10:27:12 heikki Exp $
+   Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002,2003,2004
+   Index Data Aps
+
+This file is part of the Zebra server.
+
+Zebra is free software; you can redistribute it and/or modify it under
+the terms of the GNU General Public License as published by the Free
+Software Foundation; either version 2, or (at your option) any later
+version.
+
+Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
+WARRANTY; without even the implied warranty of MERCHANTABILITY or
+FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+for more details.
+
+You should have received a copy of the GNU General Public License
+along with Zebra; see the file LICENSE.zebra.  If not, write to the
+Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
+02111-1307, USA.
+*/
+
+
 #include <stdio.h>
 #include <assert.h>
 #include <string.h>
+#include <dlfcn.h>
 
+#include <direntz.h>
 #include <zebrautl.h>
-#include "rectext.h"
-#include "recgrs.h"
+#include <idzebra/recctrl.h>
 
-struct recTypeEntry {
+struct recTypeClass {
     RecType recType;
-    struct recTypeEntry *next;
+    struct recTypeClass *next;
+    void *module_handle;
+};
+
+struct recTypeInstance {
+    RecType recType;
+    struct recTypeInstance *next;
     int init_flag;
     void *clientData;
 };
 
 struct recTypes {
     data1_handle dh;
-    struct recTypeEntry *entries;
+    struct recTypeInstance *entries;
 };
 
-RecTypes recTypes_init (data1_handle dh)
+#ifdef IDZEBRA_STATIC_TEXT
+    extern RecType idzebra_filter_text[];
+#endif
+#ifdef IDZEBRA_STATIC_GRS_XML
+#if HAVE_EXPAT_H
+    extern RecType idzebra_filter_grs_xml[];
+#endif
+#endif
+#ifdef IDZEBRA_STATIC_GRS_REGX
+    extern RecType idzebra_filter_grs_regx[];
+#endif
+#ifdef IDZEBRA_STATIC_GRS_MARC
+    extern RecType idzebra_filter_grs_marc[];
+#endif
+#ifdef IDZEBRA_STATIC_GRS_DANBIB
+    extern RecType idzebra_filter_grs_danbib[];
+#endif
+
+static void recTypeClass_add (struct recTypeClass **rts, RecType *rt,
+                             NMEM nmem, void *module_handle);
+
+RecTypeClass recTypeClass_create (Res res, NMEM nmem)
 {
-    RecTypes p = (RecTypes) nmem_malloc (data1_nmem_get (dh), sizeof(*p));
+    struct recTypeClass *rts = 0;
+    const char *module_path = res_get_def(res, "modulePath",
+                                         DEFAULT_MODULE_PATH);
+
+    extern RecType idzebra_filter_grs_sgml[];
+    recTypeClass_add (&rts, idzebra_filter_grs_sgml, nmem, 0);
+#ifdef IDZEBRA_STATIC_TEXT
+    recTypeClass_add (&rts, idzebra_filter_text, nmem, 0);
+#endif
+#ifdef IDZEBRA_STATIC_GRS_XML
+#if HAVE_EXPAT_H
+    recTypeClass_add (&rts, idzebra_filter_grs_xml, nmem, 0);
+#endif
+#endif
+#ifdef IDZEBRA_STATIC_GRS_REGX
+    recTypeClass_add (&rts, idzebra_filter_grs_regx, nmem, 0);
+#endif
+#ifdef IDZEBRA_STATIC_GRS_MARC
+    recTypeClass_add (&rts, idzebra_filter_grs_marc, nmem, 0);
+#endif
+#ifdef IDZEBRA_STATIC_GRS_DANBIB
+    recTypeClass_add (&rts, idzebra_filter_grs_danbib, nmem, 0);
+#endif
 
-    p->dh = dh;
-    p->entries = 0;
-    return p;
+    if (module_path)
+    {
+       DIR *dir = opendir(module_path);
+       yaz_log(YLOG_LOG, "searching filters in %s", module_path);
+       if (dir)
+       {
+           struct dirent *de;
+
+           while ((de = readdir(dir)))
+           {
+               size_t dlen = strlen(de->d_name);
+               if (dlen >= 5 &&
+                   !memcmp(de->d_name, "mod-", 4) &&
+                   !strcmp(de->d_name + dlen - 3, ".so"))
+               {
+                   void *mod_p, *fl;
+                   char fname[FILENAME_MAX*2+1];
+                   sprintf(fname, "%.*s/%.*s",
+                           FILENAME_MAX, module_path,
+                           FILENAME_MAX, de->d_name);
+                   mod_p = dlopen(fname, RTLD_NOW|RTLD_GLOBAL);
+                   if (mod_p && (fl = dlsym(mod_p, "idzebra_filter")))
+                   {
+                       yaz_log(YLOG_LOG, "Loaded filter module %s", fname);
+                       recTypeClass_add(&rts, fl, nmem, mod_p);
+                   }
+                   else if (mod_p)
+                   {
+                       const char *err = dlerror();
+                       yaz_log(YLOG_WARN, "dlsym failed %s %s",
+                               fname, err ? err : "none");
+                       dlclose(mod_p);
+                   }
+                   else
+                   {
+                       const char *err = dlerror();
+                       yaz_log(YLOG_WARN, "dlopen failed %s %s",
+                               fname, err ? err : "none");
+                       
+                   }
+               }
+           }
+           closedir(dir);
+       }
+    }
+    return rts;
 }
 
-void recTypes_destroy (RecTypes rts)
+static void recTypeClass_add (struct recTypeClass **rts, RecType *rt,
+                             NMEM nmem, void *module_handle)
 {
-    struct recTypeEntry *rte;
+    while (*rt)
+    {
+       struct recTypeClass *r = (struct recTypeClass *)
+           nmem_malloc (nmem, sizeof(*r));
+       
+       r->next = *rts;
+       *rts = r;
 
-    for (rte = rts->entries; rte; rte = rte->next)
-       if (rte->init_flag)
-           (*(rte->recType)->destroy)(rte->clientData);
+       yaz_log(YLOG_LOG, "Adding filter %s", (*rt)->name);
+       r->module_handle = module_handle;
+       module_handle = 0; /* so that we only store module_handle once */
+       r->recType = *rt;
+
+       rt++;
+    }
 }
 
-void recTypes_add_handler (RecTypes rts, RecType rt)
+void recTypeClass_info(RecTypeClass rtc, void *cd,
+                      void (*cb)(void *cd, const char *s))
 {
-    struct recTypeEntry *rte;
+    for (; rtc; rtc = rtc->next)
+       (*cb)(cd, rtc->recType->name);
+}
 
-    rte = (struct recTypeEntry *)
-       nmem_malloc (data1_nmem_get (rts->dh), sizeof(*rte));
+void recTypeClass_destroy(RecTypeClass rtc)
+{
+    for (; rtc; rtc = rtc->next)
+    {
+       if (rtc->module_handle)
+           dlclose(rtc->module_handle);
+    }
+}
 
-    rte->recType = rt;
-    rte->init_flag = 0;
-    rte->clientData = 0;
-    rte->next = rts->entries;
-    rts->entries = rte;
+RecTypes recTypes_init(RecTypeClass rtc, data1_handle dh)
+{
+    RecTypes rts = (RecTypes) nmem_malloc(data1_nmem_get(dh), sizeof(*rts));
+
+    struct recTypeInstance **rti = &rts->entries;
+    
+    rts->dh = dh;
+
+    for (; rtc; rtc = rtc->next)
+    {
+       *rti = nmem_malloc(data1_nmem_get(dh), sizeof(**rti));
+       (*rti)->recType = rtc->recType;
+       (*rti)->init_flag = 0;
+       rti = &(*rti)->next;
+    }
+    *rti = 0;
+    return rts;
 }
 
-RecType recType_byName (RecTypes rts, const char *name, char *subType,
-                       void **clientDataP)
+void recTypes_destroy (RecTypes rts)
 {
-    struct recTypeEntry *rte;
-    char *p;
-    char tmpname[256];
+    struct recTypeInstance *rti;
 
-    strcpy (tmpname, name);
-    if ((p = strchr (tmpname, '.')))
+    for (rti = rts->entries; rti; rti = rti->next)
     {
-        *p = '\0';
-        strcpy (subType, p+1);
+       if (rti->init_flag)
+           (*(rti->recType)->destroy)(rti->clientData);
     }
-    else
-        *subType = '\0';
-    for (rte = rts->entries; rte; rte = rte->next)
-       if (!strcmp (rte->recType->name, tmpname))
+}
+
+RecType recType_byName (RecTypes rts, Res res, const char *name,
+                       void **clientDataP)
+{
+    struct recTypeInstance *rti;
+
+    for (rti = rts->entries; rti; rti = rti->next)
+    {
+       size_t slen = strlen(rti->recType->name);
+       if (!strncmp (rti->recType->name, name, slen)
+           && (name[slen] == '\0' || name[slen] == '.'))
        {
-           if (!rte->init_flag)
+           if (!rti->init_flag)
            {
-               rte->init_flag = 1;
-               rte->clientData =
-                   (*(rte->recType)->init)(rte->recType);
+               rti->init_flag = 1;
+               rti->clientData =
+                   (*(rti->recType)->init)(res, rti->recType);
            }
-           *clientDataP = rte->clientData;
-           return rte->recType;
+           *clientDataP = rti->clientData;
+           if (name[slen])
+               slen++;  /* skip . */
+
+           if (rti->recType->config)
+               (*(rti->recType)->config)(rti->clientData, res, name+slen);
+           return rti->recType;
        }
+    }
     return 0;
 }
 
-void recTypes_default_handlers (RecTypes rts)
-{
-    recTypes_add_handler (rts, recTypeGrs);
-    recTypes_add_handler (rts, recTypeText);
-}