X-Path attribute expressions with spaces in them is now handled.
[idzebra-moved-to-github.git] / index / zrpn.c
index 80d66c7..f09278d 100644 (file)
@@ -1,5 +1,5 @@
-/* $Id: zrpn.c,v 1.125 2002-10-03 10:16:23 adam Exp $
-   Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002
+/* $Id: zrpn.c,v 1.135 2004-01-15 13:31:31 adam Exp $
+   Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002,2003,2004
    Index Data Aps
 
 This file is part of the Zebra server.
@@ -31,6 +31,7 @@ Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
 #include <ctype.h>
 
 #include "index.h"
+#include <zebra_xpath.h>
 
 #include <charmap.h>
 #include <rstemp.h>
@@ -43,6 +44,14 @@ struct rpn_char_map_info {
     int reg_type;
 };
 
+typedef struct {
+    int type;
+    int major;
+    int minor;
+    Z_AttributesPlusTerm *zapt;
+} AttrType;
+
+
 static const char **rpn_char_map_handler (void *vp, const char **from, int len)
 {
     struct rpn_char_map_info *p = (struct rpn_char_map_info *) vp;
@@ -70,32 +79,17 @@ static void rpn_char_map_prepare (struct zebra_register *reg, int reg_type,
     dict_grep_cmap (reg->dict, map_info, rpn_char_map_handler);
 }
 
-typedef struct {
-    int type;
-    int major;
-    int minor;
-    Z_AttributesPlusTerm *zapt;
-} AttrType;
-
 static int attr_find_ex (AttrType *src, oid_value *attributeSetP,
                         const char **string_value)
 {
     int num_attributes;
 
-#ifdef ASN_COMPILED
     num_attributes = src->zapt->attributes->num_attributes;
-#else
-    num_attributes = src->zapt->num_attributes;
-#endif
     while (src->major < num_attributes)
     {
         Z_AttributeElement *element;
 
-#ifdef ASN_COMPILED
         element = src->zapt->attributes->attributes[src->major];
-#else
-        element = src->zapt->attributeList[src->major];
-#endif
         if (src->type == *element->attributeType)
         {
             switch (element->which) 
@@ -453,18 +447,43 @@ static int term_104 (ZebraMaps zebra_maps, int reg_type,
     int i = 0;
     int j = 0;
 
-    if (!term_pre (zebra_maps, reg_type, src, "#!", "#!"))
+    if (!term_pre (zebra_maps, reg_type, src, "?*#", "?*#"))
         return 0;
     s0 = *src;
     while (*s0)
     {
-        if (*s0 == '#')
+        if (*s0 == '?')
+        {
+           dst_term[j++] = *s0++;
+            if (*s0 >= '0' && *s0 <= '9')
+            {
+                int limit = 0;
+                while (*s0 >= '0' && *s0 <= '9')
+                {
+                    limit = limit * 10 + (*s0 - '0');
+                    dst_term[j++] = *s0++;
+                }
+                if (limit > 20)
+                    limit = 20;
+                while (--limit >= 0)
+                {
+                    dst[i++] = '.';
+                    dst[i++] = '?';
+                }
+            }
+            else
+            {
+                dst[i++] = '.';
+                dst[i++] = '*';
+            }
+        }
+        else if (*s0 == '*')
         {
             dst[i++] = '.';
             dst[i++] = '*';
            dst_term[j++] = *s0++;
         }
-        else if (*s0 == '!')
+        else if (*s0 == '#')
        {
             dst[i++] = '.';
            dst_term[j++] = *s0++;
@@ -680,7 +699,7 @@ static int string_relation (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
     int relation_value;
     int i;
     char *term_tmp = term_dict + strlen(term_dict);
-    char term_component[256];
+    char term_component[2*IT_MAX_WORD+20];
 
     attr_init (&relation, zapt, 2);
     relation_value = attr_find (&relation, NULL);
@@ -714,6 +733,9 @@ static int string_relation (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
            *term_tmp++ = ']';
            *term_tmp++ = '.';
            *term_tmp++ = '*';
+
+            if ((term_tmp - term_dict) > IT_MAX_WORD)
+                break;
        }
        *term_tmp++ = ')';
        *term_tmp = '\0';
@@ -743,6 +765,9 @@ static int string_relation (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
            *term_tmp++ = '*';
 
            *term_tmp++ = '|';
+
+            if ((term_tmp - term_dict) > IT_MAX_WORD)
+                break;
        }
        for (i = 0; term_component[i]; )
            string_rel_add_char (&term_tmp, term_component, &i);
@@ -773,6 +798,9 @@ static int string_relation (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
            *term_tmp++ = '*';
 
            *term_tmp++ = '|';
+
+            if ((term_tmp - term_dict) > IT_MAX_WORD)
+                break;
        }
        for (i = 0; term_component[i];)
            string_rel_add_char (&term_tmp, term_component, &i);
@@ -812,6 +840,9 @@ static int string_relation (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
            *term_tmp++ = ']';
            *term_tmp++ = '.';
            *term_tmp++ = '*';
+
+            if ((term_tmp - term_dict) > IT_MAX_WORD)
+                break;
        }
        *term_tmp++ = ')';
        *term_tmp = '\0';
@@ -881,6 +912,10 @@ static int string_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
     struct rpn_char_map_info rcmi;
     int space_split = complete_flag ? 0 : 1;
 
+    int bases_ok = 0;     /* no of databases with OK attribute */
+    int errCode = 0;      /* err code (if any is not OK) */
+    char *errString = 0;  /* addinfo */
+
     rpn_char_map_prepare (zh->reg, reg_type, &rcmi);
     attr_init (&use, zapt, 1);
     use_value = attr_find_ex (&use, &curAttributeSet, &use_string);
@@ -931,9 +966,9 @@ static int string_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
                 {
                     /* set was found, but value wasn't defined */
                     char val_str[32];
-                    sprintf (val_str, "%d (1)", use_value);
-                    zh->errCode = 114;
-                    zh->errString = nmem_strdup (stream, val_str);
+                    sprintf (val_str, "%d", use_value);
+                    errCode = 114;
+                    errString = nmem_strdup (stream, val_str);
                 }
                 else
                 {
@@ -945,10 +980,10 @@ static int string_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
                     oident.value = curAttributeSet;
                     oid_ent_to_oid (&oident, oid);
                     
-                    zh->errCode = 121;
-                    zh->errString = nmem_strdup (stream, oident.desc);
+                    errCode = 121;
+                    errString = nmem_strdup (stream, oident.desc);
                 }
-                return -1;
+                continue;
             }
         }
         for (local_attr = attp.local_attributes; local_attr;
@@ -977,11 +1012,13 @@ static int string_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
         if (!prefix_len)
         {
             char val_str[32];
-            sprintf (val_str, "%d (2)", use_value);
-            zh->errCode = 114;
-            zh->errString = nmem_strdup (stream, val_str);
-            return -1;
+            sprintf (val_str, "%d", use_value);
+            errCode = 114;
+            errString = nmem_strdup (stream, val_str);
+           continue;
         }
+       bases_ok++; /* this has OK attributes */
+
         term_dict[prefix_len++] = ')';
         term_dict[prefix_len++] = 1;
         term_dict[prefix_len++] = reg_type;
@@ -1104,6 +1141,12 @@ static int string_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
            break;
         }
     }
+    if (!bases_ok)
+    {
+       zh->errCode = errCode;
+       zh->errString = errString;
+       return -1;
+    }
     *term_sub = termp;
     logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
     return 1;
@@ -1281,7 +1324,7 @@ static RSET rpn_prox (ZebraHandle zh, RSET *rset, int rset_no,
        parms.temp_path = res_get (zh->res, "setTmpDir");
        result = rset_create (rset_kind_temp, &parms);
        rsfd_result = rset_open (result, RSETF_WRITE);
-       
+
        while (*more)
        {
            for (i = 1; i<rset_no; i++)
@@ -1330,8 +1373,9 @@ static RSET rpn_prox (ZebraHandle zh, RSET *rset, int rset_no,
        rset_temp_parms parms;
        RSFD rsfd_result;
 
-       logf (LOG_LOG, "generic prox, dist = %d, relation = %d, ordered =%d, exclusion=%d",
-             distance, relation, ordered, exclusion);
+       yaz_log (LOG_LOG, "generic prox, dist=%d, relation=%d, ordered=%d"
+                         ", exclusion=%d",
+                         distance, relation, ordered, exclusion);
        parms.rset_term = rset_term_create (prox_term, length_prox_term,
                                            flags, term_type);
        parms.rset_term->nn = min_nn;
@@ -1755,20 +1799,24 @@ static int numeric_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
                         oid_value attributeSet, struct grep_info *grep_info,
                         int reg_type, int complete_flag,
                         int num_bases, char **basenames,
-                        char *term_dst)
+                        char *term_dst, int xpath_use, NMEM stream)
 {
     char term_dict[2*IT_MAX_WORD+2];
     int r, base_no;
     AttrType use;
     int use_value;
+    const char *use_string = 0;
     oid_value curAttributeSet = attributeSet;
     const char *termp;
     struct rpn_char_map_info rcmi;
 
+    int bases_ok = 0;     /* no of databases with OK attribute */
+    int errCode = 0;      /* err code (if any is not OK) */
+    char *errString = 0;  /* addinfo */
+
     rpn_char_map_prepare (zh->reg, reg_type, &rcmi);
     attr_init (&use, zapt, 1);
-    use_value = attr_find (&use, &curAttributeSet);
-    logf (LOG_DEBUG, "numeric_term, use value %d", use_value);
+    use_value = attr_find_ex (&use, &curAttributeSet, &use_string);
 
     if (use_value == -1)
         use_value = 1016;
@@ -1776,19 +1824,43 @@ static int numeric_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
     for (base_no = 0; base_no < num_bases; base_no++)
     {
         attent attp;
+        data1_local_attribute id_xpath_attr;
         data1_local_attribute *local_attr;
         int max_pos, prefix_len = 0;
 
         termp = *term_sub;
-        if ((r=att_getentbyatt (zh, &attp, curAttributeSet, use_value)))
+        if (use_value == -2)  /* string attribute (assume IDXPATH/any) */
         {
-            logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d r=%d",
-                  curAttributeSet, use_value, r);
-           if (r == -1)
-               zh->errCode = 114;
-           else
-               zh->errCode = 121;
-            return -1;
+            use_value = xpath_use;
+            attp.local_attributes = &id_xpath_attr;
+            attp.attset_ordinal = VAL_IDXPATH;
+            id_xpath_attr.next = 0;
+            id_xpath_attr.local = use_value;
+        }
+       else if (curAttributeSet == VAL_IDXPATH)
+        {
+            attp.local_attributes = &id_xpath_attr;
+            attp.attset_ordinal = VAL_IDXPATH;
+            id_xpath_attr.next = 0;
+            id_xpath_attr.local = use_value;
+        }
+        else
+        {
+            if ((r=att_getentbyatt (zh, &attp, curAttributeSet, use_value)))
+            {
+                logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d r=%d",
+                      curAttributeSet, use_value, r);
+                if (r == -1)
+               {
+                    char val_str[32];
+                    sprintf (val_str, "%d", use_value);
+                    errString = nmem_strdup (stream, val_str);
+                    errCode = 114;
+               }
+                else
+                    errCode = 121;
+                continue;
+            }
         }
         if (zebraExplain_curDatabase (zh->reg->zei, basenames[base_no]))
         {
@@ -1821,9 +1893,13 @@ static int numeric_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
         }
         if (!prefix_len)
         {
-            zh->errCode = 114;
-            return -1;
+            char val_str[32];
+            sprintf (val_str, "%d", use_value);
+            errCode = 114;
+            errString = nmem_strdup (stream, val_str);
+            continue;
         }
+       bases_ok++;
         term_dict[prefix_len++] = ')';        
         term_dict[prefix_len++] = 1;
         term_dict[prefix_len++] = reg_type;
@@ -1834,6 +1910,12 @@ static int numeric_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
                               term_dst))
            return 0;
     }
+    if (!bases_ok)
+    {
+       zh->errCode = errCode;
+       zh->errString = errString;
+       return -1;
+    }
     *term_sub = termp;
     logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
     return 1;
@@ -1845,7 +1927,7 @@ static RSET rpn_search_APT_numeric (ZebraHandle zh,
                                    oid_value attributeSet,
                                    NMEM stream,
                                    int reg_type, int complete_flag,
-                                   const char *rank_type,
+                                   const char *rank_type, int xpath_use,
                                    int num_bases, char **basenames)
 {
     char term_dst[IT_MAX_WORD+1];
@@ -1862,7 +1944,8 @@ static RSET rpn_search_APT_numeric (ZebraHandle zh,
        grep_info.isam_p_indx = 0;
         r = numeric_term (zh, zapt, &termp, attributeSet, &grep_info,
                          reg_type, complete_flag, num_bases, basenames,
-                         term_dst);
+                         term_dst, xpath_use,
+                         stream);
         if (r < 1)
             break;
        logf (LOG_DEBUG, "term: %s", term_dst);
@@ -2014,13 +2097,8 @@ static RSET rpn_sort_spec (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
        nmem_malloc (stream, sizeof(*sks->caseSensitivity));
     *sks->caseSensitivity = 0;
 
-#ifdef ASN_COMPILED
     sks->which = Z_SortKeySpec_null;
     sks->u.null = odr_nullval ();
-#else
-    sks->missingValueAction = 0;
-#endif
-
     sort_sequence->specs[i] = sks;
 
     parms.rset_term = rset_term_create (termz, -1, rank_type,
@@ -2028,6 +2106,9 @@ static RSET rpn_sort_spec (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
     return rset_create (rset_kind_null, &parms);
 }
 
+/* pop - moved to xpath.c */
+#if 0
+
 struct xpath_predicate {
     int which;
     union {
@@ -2051,102 +2132,26 @@ struct xpath_location_step {
     struct xpath_predicate *predicate;
 };
 
+#endif
+
 static int parse_xpath(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
                        oid_value attributeSet,
-                       struct xpath_location_step *xpath, NMEM mem)
+                       struct xpath_location_step *xpath, int max, NMEM mem)
 {
     oid_value curAttributeSet = attributeSet;
     AttrType use;
     const char *use_string = 0;
-    const char *cp;
-    int no = 0;
     
     attr_init (&use, zapt, 1);
     attr_find_ex (&use, &curAttributeSet, &use_string);
 
     if (!use_string || *use_string != '/')
         return -1;
-    cp = use_string;
-    while (*cp)
-    {
-        int i = 0;
-        while (*cp && !strchr("/[",*cp))
-        {
-            i++;
-            cp++;
-        }
-        xpath[no].predicate = 0;
-        xpath[no].part = nmem_malloc (mem, i+1);
-        memcpy (xpath[no].part,  cp - i, i);
-        xpath[no].part[i] = 0;
-
-        if (*cp == '[')
-        {
-            struct xpath_predicate *p = xpath[no].predicate =
-                nmem_malloc (mem, sizeof(struct xpath_predicate));
-
-            p->which = XPATH_PREDICATE_RELATION;
-            cp++;
-            while (*cp == ' ')
-                cp++;
-
-            for (i = 0; *cp && !strchr("><=] ", *cp); i++)
-                cp++;
-            p->u.relation.name = nmem_malloc (mem, i+1);
-            memcpy (p->u.relation.name, cp - i, i);
-            p->u.relation.name[i] = 0;
-            while (*cp == ' ')
-                cp++;
-            if (*cp != ']')
-            {
-                for (i = 0; *cp && strchr(">=<!", *cp); i++)
-                    cp++;
 
-                p->u.relation.op = nmem_malloc (mem, i+1);
-                if (i)
-                    memcpy (p->u.relation.op, cp - i, i);
-                p->u.relation.op[i] = 0;
-                
-                while (*cp == ' ')
-                    cp++;
-                
-                if (strchr("\"'", *cp))
-                {
-                    cp++;
-                    for (i = 0; *cp && !strchr("\"'", *cp); i++)
-                        cp++;
-
-                    p->u.relation.value = nmem_malloc (mem, i+1);
-                    if (i)
-                        memcpy (p->u.relation.value, cp - i, i);
-                    p->u.relation.value[i] = 0;
-                    yaz_log (LOG_LOG, "value=%s", p->u.relation.value);
-
-                    cp++;
-                }                           
-                else
-                {
-                    for (i = 0; *cp && !strchr(" ]", *cp); i++)
-                        cp++;
-                    p->u.relation.value = nmem_malloc (mem, i+1);
-                    if (i)
-                        memcpy (p->u.relation.value, cp - i, i);
-                    p->u.relation.value[i] = 0;
-                }
-                while (*cp == ' ')
-                    cp++;
-            }
-            if (*cp == ']')
-                cp++;
-        } /* end of ] predicate */
-        no++;
-        if (*cp != '/')
-            break;
-        cp++;
-    }
-    return no;
+    return zebra_parse_xpath_str(use_string, xpath, max, mem);
 }
-                
+               
 
 static RSET xpath_trunc(ZebraHandle zh, NMEM stream,
                         int reg_type, const char *term, int use,
@@ -2307,18 +2312,25 @@ static RSET rpn_search_xpath (ZebraHandle zh,
                 xpath[level].predicate->which == XPATH_PREDICATE_RELATION &&
                 xpath[level].predicate->u.relation.name[0])
             {
-                char predicate_str[128];
-
-                strcpy (predicate_str,
-                        xpath[level].predicate->u.relation.name+1);
+               WRBUF wbuf = wrbuf_alloc();
+               wrbuf_puts(wbuf, xpath[level].predicate->u.relation.name+1);
                 if (xpath[level].predicate->u.relation.value)
                 {
-                    strcat (predicate_str, "=");
-                    strcat (predicate_str,
-                            xpath[level].predicate->u.relation.value);
+                   const char *cp = xpath[level].predicate->u.relation.value;
+                   wrbuf_putc(wbuf, '=');
+                   
+                   while (*cp)
+                   {
+                       if (strchr(REGEX_CHARS, *cp))
+                           wrbuf_putc(wbuf, '\\');
+                       wrbuf_putc(wbuf, *cp);
+                       cp++;
+                   }
                 }
+               wrbuf_puts(wbuf, "");
                 rset_attr = xpath_trunc (
-                    zh, stream, '0', predicate_str, 3, curAttributeSet);
+                    zh, stream, '0', wrbuf_buf(wbuf), 3, curAttributeSet);
+               wrbuf_free(wbuf, 1);
             } 
             else 
             {
@@ -2382,7 +2394,7 @@ static RSET rpn_search_APT (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
     if (sort_flag)
        return rpn_sort_spec (zh, zapt, attributeSet, stream, sort_sequence,
                              rank_type);
-    xpath_len = parse_xpath(zh, zapt, attributeSet, xpath, stream);
+    xpath_len = parse_xpath(zh, zapt, attributeSet, xpath, 10, stream);
     if (xpath_len >= 0)
     {
         xpath_use = 1016;
@@ -2420,6 +2432,7 @@ static RSET rpn_search_APT (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
     {
        rset = rpn_search_APT_numeric (zh, zapt, termz, attributeSet, stream,
                                       reg_id, complete_flag, rank_type,
+                                       xpath_use,
                                       num_bases, basenames);
     }
     else if (!strcmp (search_type, "always"))
@@ -2473,21 +2486,11 @@ static RSET rpn_search_structure (ZebraHandle zh, Z_RPNStructure *zs,
             r = rset_create (rset_kind_not, &bool_parms);
             break;
         case Z_Operator_prox:
-#ifdef ASN_COMPILED
             if (zop->u.prox->which != Z_ProximityOperator_known)
             {
                 zh->errCode = 132;
                 return NULL;
             }
-#else
-            if (zop->u.prox->which != Z_ProxCode_known)
-            {
-                zh->errCode = 132;
-                return NULL;
-            }
-#endif
-
-#ifdef ASN_COMPILED
             if (*zop->u.prox->u.known != Z_ProxUnit_word)
             {
                 char *val = (char *) nmem_malloc (stream, 16);
@@ -2496,16 +2499,6 @@ static RSET rpn_search_structure (ZebraHandle zh, Z_RPNStructure *zs,
                 sprintf (val, "%d", *zop->u.prox->u.known);
                 return NULL;
             }
-#else
-            if (*zop->u.prox->proximityUnitCode != Z_ProxUnit_word)
-            {
-                char *val = (char *) nmem_malloc (stream, 16);
-                zh->errCode = 132;
-                zh->errString = val;
-                sprintf (val, "%d", *zop->u.prox->proximityUnitCode);
-                return NULL;
-            }
-#endif
            else
            {
                RSET rsets[2];
@@ -2549,6 +2542,8 @@ static RSET rpn_search_structure (ZebraHandle zh, Z_RPNStructure *zs,
                    nmem_strdup (stream, zs->u.simple->u.resultSetId);
                return 0;
            }
+            else
+                rset_dup(r);
         }
         else
         {
@@ -2709,7 +2704,7 @@ void rpn_scan (ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
               oid_value attributeset,
               int num_bases, char **basenames,
               int *position, int *num_entries, ZebraScanEntry **list,
-              int *is_partial)
+              int *is_partial, RSET limit_set, int return_zero)
 {
     int i;
     int pos = *position;
@@ -2725,16 +2720,47 @@ void rpn_scan (ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
     int ords[32], ord_no = 0;
     int ptr[32];
 
+    int bases_ok = 0;     /* no of databases with OK attribute */
+    int errCode = 0;      /* err code (if any is not OK) */
+    char *errString = 0;  /* addinfo */
+
     unsigned reg_id;
     char *search_type = NULL;
     char rank_type[128];
     int complete_flag;
     int sort_flag;
+
     *list = 0;
 
     if (attributeset == VAL_NONE)
         attributeset = VAL_BIB1;
 
+    if (!limit_set)
+    {
+        AttrType termset;
+        int termset_value_numeric;
+        const char *termset_value_string;
+        attr_init (&termset, zapt, 8);
+        termset_value_numeric =
+            attr_find_ex (&termset, NULL, &termset_value_string);
+        if (termset_value_numeric != -1)
+        {
+            char resname[32];
+            const char *termset_name = 0;
+            
+            if (termset_value_numeric != -2)
+            {
+                
+                sprintf (resname, "%d", termset_value_numeric);
+                termset_name = resname;
+            }
+            else
+                termset_name = termset_value_string;
+            
+            limit_set = resultSetRef (zh, termset_name);
+        }
+    }
+        
     yaz_log (LOG_DEBUG, "position = %d, num = %d set=%d",
              pos, num, attributeset);
         
@@ -2763,11 +2789,15 @@ void rpn_scan (ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
             logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d",
                   attributeset, use_value);
            if (r == -1)
-               zh->errCode = 114;
+           {
+                char val_str[32];
+                sprintf (val_str, "%d", use_value);
+                errCode = 114;
+                errString = odr_strdup (stream, val_str);
+           }   
            else
-               zh->errCode = 121;
-           *num_entries = 0;
-           return;
+               errCode = 121;
+           continue;
         }
         if (zebraExplain_curDatabase (zh->reg->zei, basenames[base_no]))
         {
@@ -2776,6 +2806,7 @@ void rpn_scan (ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
            *num_entries = 0;
            return;
         }
+       bases_ok++;
         for (local_attr = attp.local_attributes; local_attr && ord_no < 32;
              local_attr = local_attr->next)
         {
@@ -2787,10 +2818,20 @@ void rpn_scan (ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
                 ords[ord_no++] = ord;
         }
     }
+    if (!bases_ok && errCode)
+    {
+       zh->errCode = errCode;
+       zh->errString = errString;
+       *num_entries = 0;
+    }
     if (ord_no == 0)
     {
+       char val_str[32];
+       sprintf (val_str, "%d", use_value);
+       zh->errCode = 114;
+       zh->errString = odr_strdup (stream, val_str);
+
        *num_entries = 0;
-        zh->errCode = 113;
        return;
     }
     /* prepare dictionary scanning */
@@ -2886,6 +2927,17 @@ void rpn_scan (ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
                 ptr[j]++;
             }
         }
+        if (limit_set)
+        {
+            rset_bool_parms bool_parms;
+
+            bool_parms.key_size = sizeof(struct it_key);
+            bool_parms.cmp = key_compare_it;
+            bool_parms.rset_l = rset;
+            bool_parms.rset_r = rset_dup(limit_set);
+
+            rset = rset_create (rset_kind_and, &bool_parms);
+        }
         count_set (rset, &glist[i+before].occurrences);
         rset_delete (rset);
     }
@@ -2954,6 +3006,17 @@ void rpn_scan (ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
                 ptr[j]++;
             }
         }
+        if (limit_set)
+        {
+            rset_bool_parms bool_parms;
+
+            bool_parms.key_size = sizeof(struct it_key);
+            bool_parms.cmp = key_compare_it;
+            bool_parms.rset_l = rset;
+            bool_parms.rset_r = rset_dup(limit_set);
+
+            rset = rset_create (rset_kind_and, &bool_parms);
+        }
         count_set (rset, &glist[before-1-i].occurrences);
         rset_delete (rset);
     }