Fixed problem with element level in reading of abstract syntax.
[yaz-moved-to-github.git] / retrieval / d1_absyn.c
index a89eb91..75f69bd 100644 (file)
@@ -1,10 +1,27 @@
 /*
- * Copyright (c) 1995-1999, 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.25  1999-10-21 12:06:29  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
 #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
 
@@ -258,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
@@ -278,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))
@@ -288,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)
@@ -309,6 +339,66 @@ void fix_element_ref (data1_handle dh, data1_absyn *absyn, data1_element *e)
     }
 }
 
+
+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;
@@ -385,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;
 
@@ -394,14 +484,14 @@ data1_absyn *data1_read_absyn (data1_handle dh, const char *file)
                else
                    break;
            }
-           if (i > level + 1)
+           if (i > level+1)
            {
                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;
@@ -409,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])
@@ -463,51 +553,15 @@ data1_absyn *data1_read_absyn (data1_handle dh, const char *file)
            }
            /* 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)))
-                   {
-                       yaz_log(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)))
-                   {
-                       yaz_log(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);
@@ -536,60 +590,24 @@ data1_absyn *data1_read_absyn (data1_handle dh, const char *file)
        }
        else if (!strcmp(cmd, "all"))
        {
-           char *p;
            data1_termlist **tp = &all;
-           
            if (all)
            {
                yaz_log(LOG_WARN, "%s:%d: Too many 'all' directives - ignored",
                     file, lineno);
                continue;
            }
-
            if (argc != 2)
            {
                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)))
-               {
-                   yaz_log(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)))
-               {
-                   yaz_log(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"))
        {