+static ZEBRA_RES rpn_sort_spec(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
+ oid_value attributeSet, NMEM stream,
+ Z_SortKeySpecList *sort_sequence,
+ const char *rank_type,
+ NMEM rset_nmem,
+ RSET *rset,
+ struct rset_key_control *kc)
+{
+ int i;
+ int sort_relation_value;
+ AttrType sort_relation_type;
+ Z_SortKeySpec *sks;
+ Z_SortKey *sk;
+ int oid[OID_SIZE];
+ oident oe;
+ char termz[20];
+
+ attr_init(&sort_relation_type, zapt, 7);
+ sort_relation_value = attr_find(&sort_relation_type, &attributeSet);
+
+ if (!sort_sequence->specs)
+ {
+ sort_sequence->num_specs = 10;
+ sort_sequence->specs = (Z_SortKeySpec **)
+ nmem_malloc(stream, sort_sequence->num_specs *
+ sizeof(*sort_sequence->specs));
+ for (i = 0; i<sort_sequence->num_specs; i++)
+ sort_sequence->specs[i] = 0;
+ }
+ if (zapt->term->which != Z_Term_general)
+ i = 0;
+ else
+ i = atoi_n ((char *) zapt->term->u.general->buf,
+ zapt->term->u.general->len);
+ if (i >= sort_sequence->num_specs)
+ i = 0;
+ sprintf(termz, "%d", i);
+
+ oe.proto = PROTO_Z3950;
+ oe.oclass = CLASS_ATTSET;
+ oe.value = attributeSet;
+ if (!oid_ent_to_oid (&oe, oid))
+ return ZEBRA_FAIL;
+
+ sks = (Z_SortKeySpec *) nmem_malloc(stream, sizeof(*sks));
+ sks->sortElement = (Z_SortElement *)
+ nmem_malloc(stream, sizeof(*sks->sortElement));
+ sks->sortElement->which = Z_SortElement_generic;
+ sk = sks->sortElement->u.generic = (Z_SortKey *)
+ nmem_malloc(stream, sizeof(*sk));
+ sk->which = Z_SortKey_sortAttributes;
+ sk->u.sortAttributes = (Z_SortAttributes *)
+ nmem_malloc(stream, sizeof(*sk->u.sortAttributes));
+
+ sk->u.sortAttributes->id = oid;
+ sk->u.sortAttributes->list = zapt->attributes;
+
+ sks->sortRelation = (int *)
+ nmem_malloc(stream, sizeof(*sks->sortRelation));
+ if (sort_relation_value == 1)
+ *sks->sortRelation = Z_SortKeySpec_ascending;
+ else if (sort_relation_value == 2)
+ *sks->sortRelation = Z_SortKeySpec_descending;
+ else
+ *sks->sortRelation = Z_SortKeySpec_ascending;
+
+ sks->caseSensitivity = (int *)
+ nmem_malloc(stream, sizeof(*sks->caseSensitivity));
+ *sks->caseSensitivity = 0;
+
+ sks->which = Z_SortKeySpec_null;
+ sks->u.null = odr_nullval ();
+ sort_sequence->specs[i] = sks;
+ *rset = rsnull_create (rset_nmem, kc, 0);
+ return ZEBRA_OK;
+}
+
+
+static int parse_xpath(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
+ oid_value attributeSet,
+ struct xpath_location_step *xpath, int max, NMEM mem)
+{
+ oid_value curAttributeSet = attributeSet;
+ AttrType use;
+ const char *use_string = 0;
+
+ attr_init(&use, zapt, 1);
+ attr_find_ex(&use, &curAttributeSet, &use_string);
+
+ if (!use_string || *use_string != '/')
+ return -1;
+
+ 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,
+ oid_value curAttributeSet, NMEM rset_nmem,
+ struct rset_key_control *kc)
+{
+ RSET rset;
+ struct grep_info grep_info;
+ char term_dict[2048];
+ char ord_buf[32];
+ int prefix_len = 0;
+ int ord = zebraExplain_lookup_attr_su(zh->reg->zei, reg_type,
+ curAttributeSet, use);
+ int ord_len, i, r, max_pos;
+ int term_type = Z_Term_characterString;
+ const char *flags = "void";
+
+ if (grep_info_prepare(zh, 0 /* zapt */, &grep_info, '0') == ZEBRA_FAIL)
+ return rsnull_create(rset_nmem, kc, 0);
+
+ if (ord < 0)
+ return rsnull_create(rset_nmem, kc, 0);
+ 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];
+ }
+ term_dict[prefix_len++] = ')';
+ strcpy(term_dict+prefix_len, term);
+
+ grep_info.isam_p_indx = 0;
+ r = dict_lookup_grep(zh->reg->dict, term_dict, 0,
+ &grep_info, &max_pos, 0, grep_handle);
+ yaz_log(YLOG_DEBUG, "%s %d positions", term,
+ grep_info.isam_p_indx);
+ rset = rset_trunc(zh, grep_info.isam_p_buf,
+ grep_info.isam_p_indx, term, strlen(term),
+ flags, 1, term_type,rset_nmem,
+ kc, kc->scope, 0, reg_type, 0 /* hits_limit */,
+ 0 /* term_ref_id_str */);
+ grep_info_delete(&grep_info);
+ return rset;
+}
+
+static
+ZEBRA_RES rpn_search_xpath(ZebraHandle zh,
+ oid_value attributeSet,
+ int num_bases, char **basenames,
+ NMEM stream, const char *rank_type, RSET rset,
+ int xpath_len, struct xpath_location_step *xpath,
+ NMEM rset_nmem,
+ RSET *rset_out,
+ struct rset_key_control *kc)
+{
+ oid_value curAttributeSet = attributeSet;
+ int base_no;
+ int i;
+
+ if (xpath_len < 0)
+ {
+ *rset_out = rset;
+ return ZEBRA_OK;
+ }
+
+ yaz_log(YLOG_DEBUG, "xpath len=%d", xpath_len);
+ for (i = 0; i<xpath_len; i++)
+ {
+ yaz_log(log_level_rpn, "XPATH %d %s", i, xpath[i].part);
+
+ }
+
+ curAttributeSet = VAL_IDXPATH;
+
+ /*
+ //a -> a/.*
+ //a/b -> b/a/.*
+ /a -> a/
+ /a/b -> b/a/
+
+ / -> none
+
+ a[@attr = value]/b[@other = othervalue]
+
+ /e/@a val range(e/,range(@a,freetext(w,1015,val),@a),e/)
+ /a/b val range(b/a/,freetext(w,1016,val),b/a/)
+ /a/b/@c val range(b/a/,range(@c,freetext(w,1016,val),@c),b/a/)
+ /a/b[@c = y] val range(b/a/,freetext(w,1016,val),b/a/,@c = y)
+ /a[@c = y]/b val range(a/,range(b/a/,freetext(w,1016,val),b/a/),a/,@c = y)
+ /a[@c = x]/b[@c = y] range(a/,range(b/a/,freetext(w,1016,val),b/a/,@c = y),a/,@c = x)
+
+ */
+
+ dict_grep_cmap (zh->reg->dict, 0, 0);
+
+ for (base_no = 0; base_no < num_bases; base_no++)