+ RSET *result_sets = 0;
+ int num_result_sets = 0;
+ ZEBRA_RES res =
+ term_list_trunc(zh, zapt, termz_org, attributeSet,
+ stream, reg_type, complete_flag,
+ rank_type, xpath_use,
+ num_bases, basenames,
+ rset_nmem,
+ &result_sets, &num_result_sets);
+ if (res != ZEBRA_OK)
+ return res;
+ if (num_result_sets == 0)
+ *rset = rsnull_create (rset_nmem, key_it_ctrl);
+ else if (num_result_sets == 1)
+ *rset = result_sets[0];
+ else
+ *rset = rsmulti_or_create(rset_nmem, key_it_ctrl, key_it_ctrl->scope,
+ num_result_sets, result_sets);
+ if (!*rset)
+ return ZEBRA_FAIL;
+ return ZEBRA_OK;
+}
+
+static ZEBRA_RES rpn_search_APT_and_list(ZebraHandle zh,
+ Z_AttributesPlusTerm *zapt,
+ const char *termz_org,
+ oid_value attributeSet,
+ NMEM stream,
+ int reg_type, int complete_flag,
+ const char *rank_type,
+ int xpath_use,
+ int num_bases, char **basenames,
+ NMEM rset_nmem,
+ RSET *rset)
+{
+ RSET *result_sets = 0;
+ int num_result_sets = 0;
+ ZEBRA_RES res =
+ term_list_trunc(zh, zapt, termz_org, attributeSet,
+ stream, reg_type, complete_flag,
+ rank_type, xpath_use,
+ num_bases, basenames,
+ rset_nmem,
+ &result_sets, &num_result_sets);
+ if (res != ZEBRA_OK)
+ return res;
+ if (num_result_sets == 0)
+ *rset = rsnull_create (rset_nmem, key_it_ctrl);
+ else if (num_result_sets == 1)
+ *rset = result_sets[0];
+ else
+ *rset = rsmulti_and_create(rset_nmem, key_it_ctrl, key_it_ctrl->scope,
+ num_result_sets, result_sets);
+ if (!*rset)
+ return ZEBRA_FAIL;
+ return ZEBRA_OK;
+}
+
+static int numeric_relation(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
+ const char **term_sub,
+ char *term_dict,
+ oid_value attributeSet,
+ struct grep_info *grep_info,
+ int *max_pos,
+ int reg_type,
+ char *term_dst,
+ int *error_code)
+{
+ AttrType relation;
+ int relation_value;
+ int term_value;
+ int r;
+ char *term_tmp = term_dict + strlen(term_dict);
+
+ *error_code = 0;
+ attr_init(&relation, zapt, 2);
+ relation_value = attr_find(&relation, NULL);
+
+ yaz_log(log_level_rpn, "numeric relation value=%d", relation_value);
+
+ if (!term_100(zh->reg->zebra_maps, reg_type, term_sub, term_tmp, 1,
+ term_dst))
+ return 0;
+ term_value = atoi (term_tmp);
+ switch (relation_value)
+ {
+ case 1:
+ yaz_log(log_level_rpn, "Relation <");
+ gen_regular_rel(term_tmp, term_value-1, 1);
+ break;
+ case 2:
+ yaz_log(log_level_rpn, "Relation <=");
+ gen_regular_rel(term_tmp, term_value, 1);
+ break;
+ case 4:
+ yaz_log(log_level_rpn, "Relation >=");
+ gen_regular_rel(term_tmp, term_value, 0);
+ break;
+ case 5:
+ yaz_log(log_level_rpn, "Relation >");
+ gen_regular_rel(term_tmp, term_value+1, 0);
+ break;
+ case -1:
+ case 3:
+ yaz_log(log_level_rpn, "Relation =");
+ sprintf(term_tmp, "(0*%d)", term_value);
+ break;
+ default:
+ *error_code = 117;
+ return 0;
+ }
+ yaz_log(log_level_rpn, "dict_lookup_grep: %s", term_tmp);
+ r = dict_lookup_grep(zh->reg->dict, term_dict, 0, grep_info, max_pos,
+ 0, grep_handle);
+ if (r)
+ yaz_log(YLOG_WARN, "dict_lookup_grep fail, rel = gt: %d", r);
+ yaz_log(log_level_rpn, "%d positions", grep_info->isam_p_indx);
+ return 1;
+}
+
+static ZEBRA_RES numeric_term(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
+ const char **term_sub,
+ oid_value attributeSet,
+ struct grep_info *grep_info,
+ int reg_type, int complete_flag,
+ int num_bases, char **basenames,
+ 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_ex(&use, &curAttributeSet, &use_string);
+
+ if (use_value == -1)
+ use_value = 1016;
+
+ 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;
+ int relation_error = 0;
+
+ termp = *term_sub;
+ if (use_value == -2) /* string attribute (assume IDXPATH/any) */
+ {
+ 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,
+ use_string)))
+ {
+ yaz_log(YLOG_DEBUG, "att_getentbyatt fail. set=%d use=%d r=%d",
+ curAttributeSet, use_value, r);
+ if (r == -1)
+ {
+ errCode = YAZ_BIB1_UNSUPP_USE_ATTRIBUTE;
+ if (use_string)
+ errString = nmem_strdup(stream, use_string);
+ else
+ errString = nmem_strdup_i (stream, use_value);
+ }
+ else
+ errCode = YAZ_BIB1_UNSUPP_ATTRIBUTE_SET;
+ continue;
+ }
+ }
+ if (zebraExplain_curDatabase (zh->reg->zei, basenames[base_no]))
+ {
+ zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
+ zh->errString = basenames[base_no];
+ return -1;
+ }
+ for (local_attr = attp.local_attributes; local_attr;
+ local_attr = local_attr->next)
+ {
+ int ord;
+ char ord_buf[32];
+ int i, ord_len;
+
+ ord = zebraExplain_lookup_attr_su(zh->reg->zei,
+ attp.attset_ordinal,
+ local_attr->local);
+ if (ord < 0)
+ continue;
+ if (prefix_len)
+ term_dict[prefix_len++] = '|';
+ else
+ term_dict[prefix_len++] = '(';
+
+ ord_len = key_SU_encode (ord, ord_buf);
+ for (i = 0; i<ord_len; i++)
+ {
+ term_dict[prefix_len++] = 1;
+ term_dict[prefix_len++] = ord_buf[i];
+ }
+ }
+ if (!prefix_len)
+ {
+ errCode = YAZ_BIB1_UNSUPP_USE_ATTRIBUTE;
+ errString = nmem_strdup_i(stream, use_value);
+ continue;
+ }
+ bases_ok++;
+ term_dict[prefix_len++] = ')';
+ term_dict[prefix_len++] = 1;
+ term_dict[prefix_len++] = reg_type;
+ yaz_log(YLOG_DEBUG, "reg_type = %d", term_dict[prefix_len-1]);
+ term_dict[prefix_len] = '\0';
+ if (!numeric_relation(zh, zapt, &termp, term_dict,
+ attributeSet, grep_info, &max_pos, reg_type,
+ term_dst, &relation_error))
+ {
+ if (relation_error)
+ {
+ zh->errCode = relation_error;
+ zh->errString = 0;
+ return ZEBRA_FAIL;
+ }
+ *term_sub = 0;
+ return ZEBRA_OK;
+ }
+ }
+ if (!bases_ok)
+ {
+ zh->errCode = errCode;
+ zh->errString = errString;
+ return ZEBRA_FAIL;
+ }
+ *term_sub = termp;
+ yaz_log(YLOG_DEBUG, "%d positions", grep_info->isam_p_indx);
+ return ZEBRA_OK;
+}
+
+static ZEBRA_RES rpn_search_APT_numeric(ZebraHandle zh,
+ Z_AttributesPlusTerm *zapt,
+ const char *termz,
+ oid_value attributeSet,
+ NMEM stream,
+ int reg_type, int complete_flag,
+ const char *rank_type, int xpath_use,
+ int num_bases, char **basenames,
+ NMEM rset_nmem,
+ RSET *rset)
+{
+ char term_dst[IT_MAX_WORD+1];
+ const char *termp = termz;
+ RSET *result_sets = 0;
+ int num_result_sets = 0;
+ ZEBRA_RES res;