+struct source_parser {
+ int len;
+ const char *tok;
+ const char *src;
+ int lookahead;
+};
+
+static int sp_lex(struct source_parser *sp)
+{
+ while (*sp->src == ' ')
+ (sp->src)++;
+ sp->tok = sp->src;
+ sp->len = 0;
+ while (*sp->src && !strchr("<>();,-: ", *sp->src))
+ {
+ sp->src++;
+ sp->len++;
+ }
+ if (sp->len)
+ sp->lookahead = 't';
+ else
+ {
+ sp->lookahead = *sp->src;
+ if (*sp->src)
+ sp->src++;
+ }
+ return sp->lookahead;
+}
+
+
+static int sp_expr(struct source_parser *sp, data1_node *n, RecWord *wrd)
+{
+ if (sp->lookahead != 't')
+ return 0;
+ if (sp->len == 4 && !memcmp(sp->tok, "data", sp->len))
+ {
+ if (n->which == DATA1N_data)
+ {
+ wrd->string = n->u.data.data;
+ wrd->length = n->u.data.len;
+ }
+ sp_lex(sp);
+ }
+ else if (sp->len == 3 && !memcmp(sp->tok, "tag", sp->len))
+ {
+ if (n->which == DATA1N_tag)
+ {
+ wrd->string = n->u.tag.tag;
+ wrd->length = strlen(n->u.tag.tag);
+ }
+ sp_lex(sp);
+ }
+ else if (sp->len == 4 && !memcmp(sp->tok, "attr", sp->len))
+ {
+ sp_lex(sp);
+ if (sp->lookahead != '(')
+ return 0;
+ sp_lex(sp);
+ if (sp->lookahead != 't')
+ return 0;
+
+ if (n->which == DATA1N_tag)
+ {
+ data1_xattr *p = n->u.tag.attributes;
+ while (p && strlen(p->name) != sp->len &&
+ memcmp (p->name, sp->tok, sp->len))
+ p = p->next;
+ if (p)
+ {
+ wrd->string = p->value;
+ wrd->length = strlen(p->value);
+ }
+ }
+ sp_lex(sp);
+ if (sp->lookahead != ')')
+ return 0;
+ sp_lex(sp);
+ }
+ else if (sp->len == 5 && !memcmp(sp->tok, "range", sp->len))
+ {
+ int start, len;
+ sp_lex(sp);
+ if (sp->lookahead != '(')
+ return 0;
+
+ sp_lex(sp);
+ sp_expr(sp, n, wrd);
+ if (sp->lookahead != ',')
+ return 0;
+
+ sp_lex(sp);
+ if (sp->lookahead != 't')
+ return 0;
+ start = atoi_n(sp->tok, sp->len);
+
+ sp_lex(sp);
+ if (sp->lookahead != ',')
+ return 0;
+
+ sp_lex(sp);
+ if (sp->lookahead != 't')
+ return 0;
+ len = atoi_n(sp->tok, sp->len);
+
+ sp_lex(sp);
+ if (sp->lookahead != ')')
+ return 0;
+
+ sp_lex(sp);
+ if (wrd->string && wrd->length)
+ {
+ wrd->string += start;
+ wrd->length -= start;
+ if (wrd->length > len)
+ wrd->length = len;
+ }
+ }
+ return 1;
+}
+
+static int sp_parse(data1_node *n, RecWord *wrd, const char *src)
+{
+ struct source_parser sp;
+ sp.len = 0;
+ sp.tok = 0;
+ sp.src = src;
+ sp.lookahead = 0;
+ sp_lex(&sp);
+
+ return sp_expr(&sp, n, wrd);
+}
+
+int d1_check_xpath_predicate(data1_node *n, struct xpath_predicate *p)
+{
+ int res = 1;
+ char *attname;
+ data1_xattr *attr;
+
+ if (!p) {
+ return 1;
+ } else {
+ if (p->which == XPATH_PREDICATE_RELATION) {
+ if (p->u.relation.name[0]) {
+ if (*p->u.relation.name != '@') {
+ yaz_log(LOG_WARN,
+ " Only attributes (@) are supported in xelm xpath predicates");
+ yaz_log(LOG_WARN, "predicate %s ignored", p->u.relation.name);
+ return (1);
+ }
+ attname = p->u.relation.name + 1;
+ res = 0;
+ /* looking for the attribute with a specified name */
+ for (attr = n->u.tag.attributes; attr; attr = attr->next) {
+ yaz_log(LOG_DEBUG," - attribute %s <-> %s", attname, attr->name );
+
+ if (!strcmp(attr->name, attname)) {
+ if (p->u.relation.op[0]) {
+ if (*p->u.relation.op != '=') {
+ yaz_log(LOG_WARN,
+ "Only '=' relation is supported (%s)",p->u.relation.op);
+ yaz_log(LOG_WARN, "predicate %s ignored", p->u.relation.name);
+ res = 1; break;
+ } else {
+ yaz_log(LOG_DEBUG," - value %s <-> %s",
+ p->u.relation.value, attr->value );
+ if (!strcmp(attr->value, p->u.relation.value)) {
+ res = 1; break;
+ }
+ }
+ } else {
+ /* attribute exists, no value specified */
+ res = 1; break;
+ }
+ }
+ }
+ yaz_log(LOG_DEBUG, "return %d", res);
+ return res;
+ } else {
+ return 1;
+ }
+ }
+ else if (p->which == XPATH_PREDICATE_BOOLEAN) {
+ if (!strcmp(p->u.boolean.op,"and")) {
+ return d1_check_xpath_predicate(n, p->u.boolean.left)
+ && d1_check_xpath_predicate(n, p->u.boolean.right);
+ }
+ else if (!strcmp(p->u.boolean.op,"or")) {
+ return (d1_check_xpath_predicate(n, p->u.boolean.left)
+ || d1_check_xpath_predicate(n, p->u.boolean.right));
+ } else {
+ yaz_log(LOG_WARN, "Unknown boolean relation %s, ignored",p->u.boolean.op);
+ return 1;
+ }
+ }
+ }
+ return 0;
+}
+
+