Fixed problem with element level in reading of abstract syntax.
[yaz-moved-to-github.git] / retrieval / d1_absyn.c
index 71e35de..75f69bd 100644 (file)
@@ -1,10 +1,33 @@
 /*
- * Copyright (c) 1995-1998, Index Data.
+ * Copyright (c) 1995-2000, Index Data.
  * See the file LICENSE for details.
  * Sebastian Hammer, Adam Dickmeiss
  *
  * $Log: d1_absyn.c,v $
- * Revision 1.23  1998-10-15 08:29:16  adam
+ * Revision 1.30  2000-12-05 19:07:24  adam
+ * Fixed problem with element level in reading of abstract syntax.
+ *
+ * Revision 1.29  2000/12/05 14:34:49  adam
+ * Fixed bug with termlists (introduced by previous commit).
+ *
+ * Revision 1.28  2000/12/05 12:21:45  adam
+ * Added termlist source for data1 system.
+ *
+ * Revision 1.27  1999/12/21 14:16:19  ian
+ * Changed retrieval module to allow data1 trees with no associated absyn.
+ * Also added a simple interface for extracting values from data1 trees using
+ * a string based tagpath.
+ *
+ * Revision 1.26  1999/11/30 13:47:12  adam
+ * Improved installation. Moved header files to include/yaz.
+ *
+ * Revision 1.25  1999/10/21 12:06:29  adam
+ * Retrieval module no longer uses ctype.h - functions.
+ *
+ * Revision 1.24  1999/08/27 09:40:32  adam
+ * Renamed logf function to yaz_log. Removed VC++ project files.
+ *
+ * Revision 1.23  1998/10/15 08:29:16  adam
  * Tag set type may be specified in reference to it using "tagset"
  * directive in .abs-files and "include" directive in .tag-files.
  *
  *
  */
 
-#include <ctype.h>
 #include <stdio.h>
 #include <assert.h>
 #include <stdlib.h>
 #include <string.h>
 
-#include <oid.h>
-#include <log.h>
-#include <data1.h>
+#include <yaz/oid.h>
+#include <yaz/log.h>
+#include <yaz/data1.h>
 
 #define D1_MAX_NESTING  128
 
@@ -212,7 +234,7 @@ data1_attset *data1_attset_add (data1_handle dh, const char *name)
            *cp = '\0';
     }
     if (!attset)
-       logf (LOG_WARN|LOG_ERRNO, "Couldn't load attribute set %s", name);
+       yaz_log (LOG_WARN|LOG_ERRNO, "Couldn't load attribute set %s", name);
     else
     {
        data1_attset_cache p = (data1_attset_cache)
@@ -253,6 +275,10 @@ data1_element *data1_getelementbytagname (data1_handle dh, data1_absyn *abs,
 {
     data1_element *r;
 
+    /* It's now possible to have a data1 tree with no abstract syntax */
+    if ( !abs )
+        return 0;
+
     if (!parent)
         r = abs->main_elements;
     else
@@ -273,6 +299,11 @@ data1_element *data1_getelementbyname (data1_handle dh, data1_absyn *absyn,
                                       const char *name)
 {
     data1_element *r;
+
+    /* It's now possible to have a data1 tree with no abstract syntax */
+    if ( !absyn )
+        return 0;
+    
     assert (absyn->main_elements);
     for (r = absyn->main_elements; r; r = r->next)
        if (!data1_matchstr(r->name, name))
@@ -283,6 +314,10 @@ data1_element *data1_getelementbyname (data1_handle dh, data1_absyn *absyn,
 
 void fix_element_ref (data1_handle dh, data1_absyn *absyn, data1_element *e)
 {
+    /* It's now possible to have a data1 tree with no abstract syntax */
+    if ( !absyn )
+        return;
+
     for (; e; e = e->next)
     {
        if (!e->sub_name)
@@ -298,12 +333,72 @@ void fix_element_ref (data1_handle dh, data1_absyn *absyn, data1_element *e)
            if (sub_e)
                e->children = sub_e->elements;
            else
-               logf (LOG_WARN, "Unresolved reference to sub-elements %s",
+               yaz_log (LOG_WARN, "Unresolved reference to sub-elements %s",
                      e->sub_name);
        }
     }
 }
 
+
+static int parse_termlists (data1_handle dh, data1_termlist ***tpp,
+                           char *p, const char *file, int lineno,
+                           const char *element_name, data1_absyn *res)
+{
+    data1_termlist **tp = *tpp;
+    do
+    {
+       char attname[512], structure[512];
+       char *source;
+       int r;
+       
+       if (!(r = sscanf(p, "%511[^:,]:%511[^,]", attname,
+                        structure)))
+       {
+           yaz_log(LOG_WARN,
+                   "%s:%d: Syntax error in termlistspec '%s'",
+                   file, lineno, p);
+           return -1;
+/*
+  fclose(f);
+  return 0;
+*/
+       }
+       if (*attname == '!')
+           strcpy(attname, element_name);
+       *tp = (data1_termlist *)
+           nmem_malloc(data1_nmem_get(dh), sizeof(**tp));
+       (*tp)->next = 0;
+       if (!((*tp)->att = data1_getattbyname(dh, res->attset,
+                                             attname)))
+       {
+           yaz_log(LOG_WARN,
+                   "%s:%d: Couldn't find att '%s' in attset",
+                   file, lineno, attname);
+           return -1;
+/*
+           fclose(f);
+           return 0;
+*/
+       }
+       if (r == 2 && (source = strchr(structure, ':')))
+           *source++ = '\0';   /* cut off structure .. */
+       else
+           source = "data";    /* ok: default is leaf data */
+       (*tp)->source = (char *)
+           nmem_strdup (data1_nmem_get (dh), source);
+       
+       if (r < 2) /* is the structure qualified? */
+           (*tp)->structure = "w";
+       else 
+           (*tp)->structure = (char *)
+               nmem_strdup (data1_nmem_get (dh), structure);
+       tp = &(*tp)->next;
+    }
+    while ((p = strchr(p, ',')) && *(++p));
+    *tpp = tp;
+    return 0;
+}
+
 data1_absyn *data1_read_absyn (data1_handle dh, const char *file)
 {
     data1_sub_elements *cur_elements = NULL;
@@ -323,7 +418,7 @@ data1_absyn *data1_read_absyn (data1_handle dh, const char *file)
 
     if (!(f = yaz_path_fopen(data1_get_tabpath (dh), file, "r")))
     {
-       logf(LOG_WARN|LOG_ERRNO, "Couldn't open %s", file);
+       yaz_log(LOG_WARN|LOG_ERRNO, "Couldn't open %s", file);
        return 0;
     }
     
@@ -360,7 +455,7 @@ data1_absyn *data1_read_absyn (data1_handle dh, const char *file)
 
            if (argc < 4)
            {
-               logf(LOG_WARN, "%s:%d: Bad # of args to elm", file, lineno);
+               yaz_log(LOG_WARN, "%s:%d: Bad # of args to elm", file, lineno);
                continue;
            }
            path = argv[1];
@@ -380,7 +475,7 @@ data1_absyn *data1_read_absyn (data1_handle dh, const char *file)
                ppl[level] = &cur_elements->elements;
             }
            p = path;
-           for (i = 0;; i++)
+           for (i = 1;; i++)
            {
                char *e;
 
@@ -389,14 +484,14 @@ data1_absyn *data1_read_absyn (data1_handle dh, const char *file)
                else
                    break;
            }
-           if (i > level + 1)
+           if (i > level+1)
            {
-               logf(LOG_WARN, "%s:%d: Bad level increase", file, lineno);
+               yaz_log(LOG_WARN, "%s:%d: Bad level increase", file, lineno);
                fclose(f);
                return 0;
            }
            level = i;
-           new_element = *ppl[level] = (data1_element *)
+           new_element = *ppl[level-1] = (data1_element *)
                nmem_malloc(data1_nmem_get(dh), sizeof(*new_element));
            new_element->next = new_element->children = 0;
            new_element->tag = 0;
@@ -404,8 +499,8 @@ data1_absyn *data1_read_absyn (data1_handle dh, const char *file)
            new_element->sub_name = 0;
            
            tp = &new_element->termlists;
-           ppl[level] = &new_element->next;
-           ppl[level+1] = &new_element->children;
+           ppl[level-1] = &new_element->next;
+           ppl[level] = &new_element->children;
            
            /* consider subtree (if any) ... */
            if ((sub_p = strchr (p, ':')) && sub_p[1])
@@ -419,14 +514,14 @@ data1_absyn *data1_read_absyn (data1_handle dh, const char *file)
            {
                if (!res->tagset)
                {
-                   logf(LOG_WARN, "%s:%d: No tagset loaded", file, lineno);
+                   yaz_log(LOG_WARN, "%s:%d: No tagset loaded", file, lineno);
                    fclose(f);
                    return 0;
                }
                if (!(new_element->tag = data1_gettagbynum (dh, res->tagset,
                                                            type, value)))
                {
-                   logf(LOG_WARN, "%s:%d: Couldn't find tag %s in tagset",
+                   yaz_log(LOG_WARN, "%s:%d: Couldn't find tag %s in tagset",
                         file, lineno, p);
                    fclose(f);
                    return 0;
@@ -452,57 +547,21 @@ data1_absyn *data1_read_absyn (data1_handle dh, const char *file)
            }
            else
            {
-               logf(LOG_WARN, "%s:%d: Bad element", file, lineno);
+               yaz_log(LOG_WARN, "%s:%d: Bad element", file, lineno);
                fclose(f);
                return 0;
            }
            /* parse termList definitions */
            p = termlists;
-           if (*p == '-')
-               new_element->termlists = 0;
-           else
+           if (*p != '-')
            {
                assert (res->attset);
-               do
+               
+               if (parse_termlists (dh, &tp, p, file, lineno, name, res))
                {
-                   char attname[512], structure[512];
-                   int r;
-                   
-                   if (!(r = sscanf(p, "%511[^:,]:%511[^,]", attname,
-                                    structure)))
-                   {
-                       logf(LOG_WARN,
-                            "%s:%d: Syntax error in termlistspec '%s'",
-                            file, lineno, p);
-                       fclose(f);
-                       return 0;
-                   }
-                   if (*attname == '!')
-                       strcpy(attname, name);
-                   *tp = (data1_termlist *)
-                       nmem_malloc(data1_nmem_get(dh), sizeof(**tp));
-                   (*tp)->next = 0;
-                   if (!((*tp)->att = data1_getattbyname(dh, res->attset,
-                                                         attname)))
-                   {
-                       logf(LOG_WARN,
-                            "%s:%d: Couldn't find att '%s' in attset",
-                            file, lineno, attname);
-                       fclose(f);
-                       return 0;
-                   }
-                   if (r < 2) /* is the structure qualified? */
-                       (*tp)->structure = "w";
-                   else 
-                   {
-                       (*tp)->structure = (char *)
-                           nmem_malloc (data1_nmem_get (dh),
-                                        strlen(structure)+1);
-                       strcpy ((*tp)->structure, structure);
-                   }
-                   tp = &(*tp)->next;
+                   fclose (f);
+                   return 0;
                }
-               while ((p = strchr(p, ',')) && *(++p));
                *tp = all; /* append any ALL entries to the list */
            }
            new_element->name = nmem_strdup(data1_nmem_get (dh), name);
@@ -513,7 +572,7 @@ data1_absyn *data1_read_absyn (data1_handle dh, const char *file)
            
            if (argc < 2)
            {
-               logf(LOG_WARN, "%s:%d: Bad # of args to section",
+               yaz_log(LOG_WARN, "%s:%d: Bad # of args to section",
                     file, lineno);
                continue;
            }
@@ -531,66 +590,30 @@ data1_absyn *data1_read_absyn (data1_handle dh, const char *file)
        }
        else if (!strcmp(cmd, "all"))
        {
-           char *p;
            data1_termlist **tp = &all;
-           
            if (all)
            {
-               logf(LOG_WARN, "%s:%d: Too many 'all' directives - ignored",
+               yaz_log(LOG_WARN, "%s:%d: Too many 'all' directives - ignored",
                     file, lineno);
                continue;
            }
-
            if (argc != 2)
            {
-               logf(LOG_WARN, "%s:%d: Bad # of args to 'all' directive",
+               yaz_log(LOG_WARN, "%s:%d: Bad # of args to 'all' directive",
                     file, lineno);
                continue;
            }
-           p = argv[1];
-           assert (res->attset);
-           do
+           if (parse_termlists (dh, &tp, argv[1], file, lineno, 0, res))
            {
-               char attname[512], structure[512];
-               int r;
-               
-               if (!(r = sscanf(p, "%511[^:,]:%511[^,]", attname,
-                                structure)))
-               {
-                   logf(LOG_WARN, "%s:%d: Syntax error in termlistspec",
-                        file, lineno);
-                   fclose(f);
-                   return 0;
-               }
-               *tp = (data1_termlist *)
-                   nmem_malloc(data1_nmem_get(dh), sizeof(**tp));
-               if (!((*tp)->att =
-                     data1_getattbyname (dh, res->attset, attname)))
-               {
-                   logf(LOG_WARN, "%s:%d: Couldn't find att '%s' in attset",
-                        file, lineno, attname);
-                   fclose(f);
-                   return 0;
-               }
-               if (r < 2) /* is the structure qualified? */
-                   (*tp)->structure = "w";
-               else 
-               {
-                   (*tp)->structure =
-                       (char *)nmem_malloc (data1_nmem_get (dh),
-                                            strlen(structure)+1);
-                   strcpy ((*tp)->structure, structure);
-               }
-               (*tp)->next = 0;
-               tp = &(*tp)->next;
+               fclose (f);
+               return 0;
            }
-           while ((p = strchr(p, ',')) && *(++p));
        }
        else if (!strcmp(cmd, "name"))
        {
            if (argc != 2)
            {
-               logf(LOG_WARN, "%s:%d: Bad # of args to name directive",
+               yaz_log(LOG_WARN, "%s:%d: Bad # of args to name directive",
                     file, lineno);
                continue;
            }
@@ -602,14 +625,14 @@ data1_absyn *data1_read_absyn (data1_handle dh, const char *file)
            
            if (argc != 2)
            {
-               logf(LOG_WARN, "%s:%d: Bad # of args to reference",
+               yaz_log(LOG_WARN, "%s:%d: Bad # of args to reference",
                     file, lineno);
                continue;
            }
            name = argv[1];
            if ((res->reference = oid_getvalbyname(name)) == VAL_NONE)
            {
-               logf(LOG_WARN, "%s:%d: Unknown tagset ref '%s'", 
+               yaz_log(LOG_WARN, "%s:%d: Unknown tagset ref '%s'", 
                     file, lineno, name);
                continue;
            }
@@ -621,14 +644,14 @@ data1_absyn *data1_read_absyn (data1_handle dh, const char *file)
            
            if (argc != 2)
            {
-               logf(LOG_WARN, "%s:%d: Bad # of args to attset",
+               yaz_log(LOG_WARN, "%s:%d: Bad # of args to attset",
                     file, lineno);
                continue;
            }
            name = argv[1];
            if (!(attset = data1_get_attset (dh, name)))
            {
-               logf(LOG_WARN, "%s:%d: Couldn't find attset  %s",
+               yaz_log(LOG_WARN, "%s:%d: Couldn't find attset  %s",
                     file, lineno, name);
                continue;
            }
@@ -644,7 +667,7 @@ data1_absyn *data1_read_absyn (data1_handle dh, const char *file)
            int type = 0;
            if (argc < 2)
            {
-               logf(LOG_WARN, "%s:%d: Bad # of args to tagset",
+               yaz_log(LOG_WARN, "%s:%d: Bad # of args to tagset",
                     file, lineno);
                continue;
            }
@@ -654,7 +677,7 @@ data1_absyn *data1_read_absyn (data1_handle dh, const char *file)
            *tagset_childp = data1_read_tagset (dh, name, type);
            if (!(*tagset_childp))
            {
-               logf(LOG_WARN, "%s:%d: Couldn't load tagset %s",
+               yaz_log(LOG_WARN, "%s:%d: Couldn't load tagset %s",
                     file, lineno, name);
                continue;
            }
@@ -666,14 +689,14 @@ data1_absyn *data1_read_absyn (data1_handle dh, const char *file)
 
            if (argc != 2)
            {
-               logf(LOG_WARN, "%s:%d: Bad # of args in varset",
+               yaz_log(LOG_WARN, "%s:%d: Bad # of args in varset",
                     file, lineno);
                continue;
            }
            name = argv[1];
            if (!(res->varset = data1_read_varset (dh, name)))
            {
-               logf(LOG_WARN, "%s:%d: Couldn't load Varset %s",
+               yaz_log(LOG_WARN, "%s:%d: Couldn't load Varset %s",
                     file, lineno, name);
                continue;
            }
@@ -684,7 +707,7 @@ data1_absyn *data1_read_absyn (data1_handle dh, const char *file)
 
            if (argc != 3)
            {
-               logf(LOG_WARN, "%s:%d: Bad # of args in esetname",
+               yaz_log(LOG_WARN, "%s:%d: Bad # of args in esetname",
                     file, lineno);
                continue;
            }
@@ -699,7 +722,7 @@ data1_absyn *data1_read_absyn (data1_handle dh, const char *file)
                (*esetpp)->spec = 0;
            else if (!((*esetpp)->spec = data1_read_espec1 (dh, fname)))
            {
-               logf(LOG_WARN, "%s:%d: Espec-1 read failed for %s",
+               yaz_log(LOG_WARN, "%s:%d: Espec-1 read failed for %s",
                     file, lineno, fname);
                continue;
            }
@@ -711,14 +734,14 @@ data1_absyn *data1_read_absyn (data1_handle dh, const char *file)
            
            if (argc != 2)
            {
-               logf(LOG_WARN, "%s:%d: Bad # of args for maptab",
+               yaz_log(LOG_WARN, "%s:%d: Bad # of args for maptab",
                      file, lineno);
                continue;
            }
            name = argv[1];
            if (!(*maptabp = data1_read_maptab (dh, name)))
            {
-               logf(LOG_WARN, "%s:%d: Couldn't load maptab %s",
+               yaz_log(LOG_WARN, "%s:%d: Couldn't load maptab %s",
                      file, lineno, name);
                continue;
            }
@@ -730,14 +753,14 @@ data1_absyn *data1_read_absyn (data1_handle dh, const char *file)
            
            if (argc != 2)
            {
-               logf(LOG_WARN, "%s:%d: Bad # or args for marc",
+               yaz_log(LOG_WARN, "%s:%d: Bad # or args for marc",
                     file, lineno);
                continue;
            }
            name = argv[1];
            if (!(*marcp = data1_read_marctab (dh, name)))
            {
-               logf(LOG_WARN, "%s:%d: Couldn't read marctab %s",
+               yaz_log(LOG_WARN, "%s:%d: Couldn't read marctab %s",
                      file, lineno, name);
                continue;
            }
@@ -745,7 +768,7 @@ data1_absyn *data1_read_absyn (data1_handle dh, const char *file)
        }
        else
        {
-           logf(LOG_WARN, "%s:%d: Unknown directive '%s'", file, lineno, cmd);
+           yaz_log(LOG_WARN, "%s:%d: Unknown directive '%s'", file, lineno, cmd);
            continue;
        }
     }
@@ -758,6 +781,6 @@ data1_absyn *data1_read_absyn (data1_handle dh, const char *file)
            res->main_elements = cur_elements->elements;
        fix_element_ref (dh, res, cur_elements->elements);
     }
-    logf (LOG_DEBUG, "%s: data1_read_absyn end", file);
+    yaz_log (LOG_DEBUG, "%s: data1_read_absyn end", file);
     return res;
 }