1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2010 Index Data
3 * See the file LICENSE for details.
7 * \brief Implements PQF parsing
14 #include <yaz/proto.h>
15 #include <yaz/oid_db.h>
16 #include <yaz/pquery.h>
18 struct yaz_pqf_parser {
19 const char *query_buf;
20 const char *query_ptr;
32 static Z_RPNStructure *rpn_structure(struct yaz_pqf_parser *li, ODR o,
33 int num_attr, int max_attr,
34 Odr_int *attr_list, char **attr_clist,
37 static Odr_oid *query_oid_getvalbyname(struct yaz_pqf_parser *li, ODR o)
41 if (li->lex_len >= sizeof(buf)-1)
43 memcpy(buf, li->lex_buf, li->lex_len);
44 buf[li->lex_len] = '\0';
45 return yaz_string_to_oid_odr(yaz_oid_std(), CLASS_ATTSET, buf, o);
48 static int compare_term(struct yaz_pqf_parser *li, const char *src,
51 size_t len=strlen(src);
53 if (li->lex_len == len+off && !memcmp(li->lex_buf+off, src, len-off))
58 static int query_token(struct yaz_pqf_parser *li)
61 const char *sep_match;
62 const char **qptr = &li->query_ptr;
69 if ((sep_match = strchr(li->left_sep, **qptr)))
71 sep_char = li->right_sep[sep_match - li->left_sep];
76 if (**qptr == li->escape_char && isdigit(((const unsigned char *) *qptr)[1]))
82 while (**qptr && **qptr != sep_char)
94 if (sep_char == ' ' &&
95 li->lex_len >= 1 && li->lex_buf[0] == li->escape_char)
97 if (compare_term(li, "and", 1))
99 if (compare_term(li, "or", 1))
101 if (compare_term(li, "not", 1))
103 if (compare_term(li, "attr", 1))
105 if (compare_term(li, "set", 1))
107 if (compare_term(li, "attrset", 1))
109 if (compare_term(li, "prox", 1))
111 if (compare_term(li, "term", 1))
117 static int lex(struct yaz_pqf_parser *li)
119 return li->query_look = query_token(li);
122 static int escape_string(char *out_buf, const char *in, int len)
127 if (*in == '\\' && len > 0)
182 return out - out_buf;
185 static int p_query_parse_attr(struct yaz_pqf_parser *li, ODR o,
186 int num_attr, Odr_int *attr_list,
187 char **attr_clist, Odr_oid **attr_set)
192 if (!(cp = strchr(li->lex_buf, '=')) ||
193 (size_t) (cp-li->lex_buf) > li->lex_len)
195 attr_set[num_attr] = query_oid_getvalbyname(li, o);
196 if (attr_set[num_attr] == 0)
198 li->error = YAZ_PQF_ERROR_ATTSET;
203 li->error = YAZ_PQF_ERROR_MISSING;
206 if (!(cp = strchr(li->lex_buf, '=')))
208 li->error = YAZ_PQF_ERROR_BADATTR;
215 attr_set[num_attr] = attr_set[num_attr-1];
217 attr_set[num_attr] = 0;
219 if (*li->lex_buf < '0' || *li->lex_buf > '9')
221 li->error = YAZ_PQF_ERROR_BAD_INTEGER;
224 attr_list[2*num_attr] = odr_atoi(li->lex_buf);
227 /* inspect value .. and make it a integer if it appears to be */
228 for (i = cp - li->lex_buf; i < li->lex_len; i++)
229 if (li->lex_buf[i] < '0' || li->lex_buf[i] > '9')
231 int len = li->lex_len - (cp - li->lex_buf);
232 attr_list[2*num_attr+1] = 0;
233 attr_clist[num_attr] = (char *) odr_malloc(o, len+1);
234 len = escape_string(attr_clist[num_attr], cp, len);
235 attr_clist[num_attr][len] = '\0';
238 attr_list[2*num_attr+1] = odr_atoi(cp);
239 attr_clist[num_attr] = 0;
243 static Z_AttributesPlusTerm *rpn_term(struct yaz_pqf_parser *li, ODR o,
244 int num_attr, Odr_int *attr_list,
245 char **attr_clist, Odr_oid **attr_set)
247 Z_AttributesPlusTerm *zapt;
250 Z_AttributeElement **elements;
252 zapt = (Z_AttributesPlusTerm *)odr_malloc(o, sizeof(*zapt));
253 term_octet = (Odr_oct *)odr_malloc(o, sizeof(*term_octet));
254 term = (Z_Term *)odr_malloc(o, sizeof(*term));
257 elements = (Z_AttributeElement**)odr_nullval();
263 elements = (Z_AttributeElement**)
264 odr_malloc (o, num_attr * sizeof(*elements));
266 attr_tmp = (Odr_int *)odr_malloc(o, num_attr * 2 * sizeof(*attr_tmp));
267 memcpy(attr_tmp, attr_list, num_attr * 2 * sizeof(*attr_tmp));
268 for (i = num_attr; --i >= 0; )
271 for (j = i+1; j<num_attr; j++)
272 if (attr_tmp[2*j] == attr_tmp[2*i])
277 (Z_AttributeElement*)odr_malloc(o,sizeof(**elements));
278 elements[k]->attributeType = &attr_tmp[2*i];
279 elements[k]->attributeSet = attr_set[i];
283 elements[k]->which = Z_AttributeValue_complex;
284 elements[k]->value.complex = (Z_ComplexAttribute *)
285 odr_malloc(o, sizeof(Z_ComplexAttribute));
286 elements[k]->value.complex->num_list = 1;
287 elements[k]->value.complex->list =
288 (Z_StringOrNumeric **)
289 odr_malloc(o, 1 * sizeof(Z_StringOrNumeric *));
290 elements[k]->value.complex->list[0] =
291 (Z_StringOrNumeric *)
292 odr_malloc(o, sizeof(Z_StringOrNumeric));
293 elements[k]->value.complex->list[0]->which =
294 Z_StringOrNumeric_string;
295 elements[k]->value.complex->list[0]->u.string =
297 elements[k]->value.complex->semanticAction = 0;
298 elements[k]->value.complex->num_semanticAction = 0;
302 elements[k]->which = Z_AttributeValue_numeric;
303 elements[k]->value.numeric = &attr_tmp[2*i+1];
309 zapt->attributes = (Z_AttributeList *)
310 odr_malloc(o, sizeof(*zapt->attributes));
311 zapt->attributes->num_attributes = num_attr;
312 zapt->attributes->attributes = elements;
316 term_octet->buf = (unsigned char *)odr_malloc(o, 1 + li->lex_len);
317 term_octet->size = term_octet->len =
318 escape_string((char *) (term_octet->buf), li->lex_buf, li->lex_len);
319 term_octet->buf[term_octet->size] = 0; /* null terminate */
321 switch (li->term_type)
324 term->which = Z_Term_general;
325 term->u.general = term_octet;
327 case Z_Term_characterString:
328 term->which = Z_Term_characterString;
329 term->u.characterString = (char*) term_octet->buf;
330 /* null terminated above */
333 term->which = Z_Term_numeric;
334 term->u.numeric = odr_intdup(o, odr_atoi((const char*) term_octet->buf));
337 term->which = Z_Term_null;
338 term->u.null = odr_nullval();
340 case Z_Term_external:
341 term->which = Z_Term_external;
342 term->u.external = 0;
345 term->which = Z_Term_null;
346 term->u.null = odr_nullval();
352 static Z_Operand *rpn_simple(struct yaz_pqf_parser *li, ODR o,
353 int num_attr, Odr_int *attr_list,
359 zo = (Z_Operand *)odr_malloc(o, sizeof(*zo));
360 switch (li->query_look)
363 zo->which = Z_Operand_APT;
364 if (!(zo->u.attributesPlusTerm =
365 rpn_term(li, o, num_attr, attr_list, attr_clist, attr_set)))
373 li->error = YAZ_PQF_ERROR_MISSING;
376 zo->which = Z_Operand_resultSetId;
377 zo->u.resultSetId = (char *)odr_malloc(o, li->lex_len+1);
378 memcpy(zo->u.resultSetId, li->lex_buf, li->lex_len);
379 zo->u.resultSetId[li->lex_len] = '\0';
383 /* we're only called if one of the above types are seens so
384 this shouldn't happen */
385 li->error = YAZ_PQF_ERROR_INTERNAL;
391 static Z_ProximityOperator *rpn_proximity(struct yaz_pqf_parser *li, ODR o)
393 Z_ProximityOperator *p = (Z_ProximityOperator *)odr_malloc(o, sizeof(*p));
397 li->error = YAZ_PQF_ERROR_MISSING;
400 if (*li->lex_buf == '1')
401 p->exclusion = odr_booldup(o, 1);
402 else if (*li->lex_buf == '0')
403 p->exclusion = odr_booldup(o, 0);
404 else if (*li->lex_buf == 'v' || *li->lex_buf == 'n')
408 li->error = YAZ_PQF_ERROR_PROXIMITY;
414 li->error = YAZ_PQF_ERROR_MISSING;
417 if (*li->lex_buf >= '0' && *li->lex_buf <= '9')
418 p->distance = odr_intdup(o, odr_atoi(li->lex_buf));
421 li->error = YAZ_PQF_ERROR_BAD_INTEGER;
427 li->error = YAZ_PQF_ERROR_MISSING;
430 if (*li->lex_buf == '1')
431 p->ordered = odr_booldup(o, 1);
432 else if (*li->lex_buf == '0')
433 p->ordered = odr_booldup(o, 0);
436 li->error = YAZ_PQF_ERROR_PROXIMITY;
442 li->error = YAZ_PQF_ERROR_MISSING;
445 if (*li->lex_buf >= '0' && *li->lex_buf <= '9')
446 p->relationType = odr_intdup(o, odr_atoi(li->lex_buf));
449 li->error = YAZ_PQF_ERROR_BAD_INTEGER;
455 li->error = YAZ_PQF_ERROR_MISSING;
458 if (*li->lex_buf == 'k')
459 p->which = Z_ProximityOperator_known;
460 else if (*li->lex_buf == 'p')
461 p->which = Z_ProximityOperator_private;
463 p->which = atoi(li->lex_buf);
465 if (p->which != Z_ProximityOperator_known
466 && p->which != Z_ProximityOperator_private)
468 li->error = YAZ_PQF_ERROR_PROXIMITY;
474 li->error = YAZ_PQF_ERROR_MISSING;
477 if (*li->lex_buf >= '0' && *li->lex_buf <= '9')
478 p->u.known = odr_intdup(o, odr_atoi(li->lex_buf));
481 li->error = YAZ_PQF_ERROR_BAD_INTEGER;
487 static Z_Complex *rpn_complex(struct yaz_pqf_parser *li, ODR o,
488 int num_attr, int max_attr,
489 Odr_int *attr_list, char **attr_clist,
495 zc = (Z_Complex *)odr_malloc(o, sizeof(*zc));
496 zo = (Z_Operator *)odr_malloc(o, sizeof(*zo));
498 switch (li->query_look)
501 zo->which = Z_Operator_and;
502 zo->u.op_and = odr_nullval();
505 zo->which = Z_Operator_or;
506 zo->u.op_or = odr_nullval();
509 zo->which = Z_Operator_and_not;
510 zo->u.and_not = odr_nullval();
513 zo->which = Z_Operator_prox;
514 zo->u.prox = rpn_proximity(li, o);
519 /* we're only called if one of the above types are seens so
520 this shouldn't happen */
521 li->error = YAZ_PQF_ERROR_INTERNAL;
526 rpn_structure(li, o, num_attr, max_attr, attr_list,
527 attr_clist, attr_set)))
530 rpn_structure(li, o, num_attr, max_attr, attr_list,
531 attr_clist, attr_set)))
536 static void rpn_term_type(struct yaz_pqf_parser *li)
540 if (compare_term(li, "general", 0))
541 li->term_type = Z_Term_general;
542 else if (compare_term(li, "numeric", 0))
543 li->term_type = Z_Term_numeric;
544 else if (compare_term(li, "string", 0))
545 li->term_type = Z_Term_characterString;
546 else if (compare_term(li, "oid", 0))
547 li->term_type = Z_Term_oid;
548 else if (compare_term(li, "datetime", 0))
549 li->term_type = Z_Term_dateTime;
550 else if (compare_term(li, "null", 0))
551 li->term_type = Z_Term_null;
553 else if (compare_term(li, "range", 0))
555 /* prepare for external: range search .. */
556 li->term_type = Z_Term_external;
557 li->external_type = VAL_MULTISRCH2;
563 static Z_RPNStructure *rpn_structure(struct yaz_pqf_parser *li, ODR o,
564 int num_attr, int max_attr,
571 sz = (Z_RPNStructure *)odr_malloc(o, sizeof(*sz));
572 switch (li->query_look)
578 sz->which = Z_RPNStructure_complex;
579 if (!(sz->u.complex =
580 rpn_complex(li, o, num_attr, max_attr, attr_list,
581 attr_clist, attr_set)))
586 sz->which = Z_RPNStructure_simple;
588 rpn_simple(li, o, num_attr, attr_list,
589 attr_clist, attr_set)))
596 li->error = YAZ_PQF_ERROR_MISSING;
599 if (num_attr >= max_attr)
601 li->error = YAZ_PQF_ERROR_TOOMANY;
604 if (!p_query_parse_attr(li, o, num_attr, attr_list,
605 attr_clist, attr_set))
610 rpn_structure(li, o, num_attr, max_attr, attr_list,
611 attr_clist, attr_set);
616 rpn_structure(li, o, num_attr, max_attr, attr_list,
617 attr_clist, attr_set);
618 case 0: /* operator/operand expected! */
619 li->error = YAZ_PQF_ERROR_MISSING;
625 static Z_RPNQuery *p_query_rpn_mk(ODR o, struct yaz_pqf_parser *li)
628 Odr_int attr_array[1024];
629 char *attr_clist[512];
630 Odr_oid *attr_set[512];
631 Odr_oid *top_set = 0;
633 zq = (Z_RPNQuery *)odr_malloc(o, sizeof(*zq));
635 if (li->query_look == 'r')
638 top_set = query_oid_getvalbyname(li, o);
641 li->error = YAZ_PQF_ERROR_ATTSET;
648 top_set = odr_oiddup(o, yaz_oid_attset_bib_1);
651 zq->attributeSetId = top_set;
653 if (!zq->attributeSetId)
655 li->error = YAZ_PQF_ERROR_ATTSET;
659 if (!(zq->RPNStructure = rpn_structure(li, o, 0, 512,
660 attr_array, attr_clist, attr_set)))
664 li->error = YAZ_PQF_ERROR_EXTRA;
670 Z_RPNQuery *p_query_rpn(ODR o, const char *qbuf)
672 struct yaz_pqf_parser li;
676 li.right_sep = "}\"";
677 li.escape_char = '@';
678 li.term_type = Z_Term_general;
679 li.query_buf = li.query_ptr = qbuf;
681 return p_query_rpn_mk(o, &li);
685 static Z_AttributesPlusTerm *p_query_scan_mk(struct yaz_pqf_parser *li,
687 Odr_oid **attributeSetP)
689 Odr_int attr_list[1024];
690 char *attr_clist[512];
691 Odr_oid *attr_set[512];
694 Odr_oid *top_set = 0;
695 Z_AttributesPlusTerm *apt;
698 if (li->query_look == 'r')
701 top_set = query_oid_getvalbyname(li, o);
704 li->error = YAZ_PQF_ERROR_ATTSET;
711 top_set = odr_oiddup(o, yaz_oid_attset_bib_1);
713 *attributeSetP = top_set;
717 if (li->query_look == 'l')
722 li->error = YAZ_PQF_ERROR_MISSING;
725 if (num_attr >= max_attr)
727 li->error = YAZ_PQF_ERROR_TOOMANY;
730 if (!p_query_parse_attr(li, o, num_attr, attr_list,
731 attr_clist, attr_set))
736 else if (li->query_look == 'y')
746 li->error = YAZ_PQF_ERROR_MISSING;
749 apt = rpn_term(li, o, num_attr, attr_list, attr_clist, attr_set);
753 if (li->query_look != 0)
755 li->error = YAZ_PQF_ERROR_EXTRA;
761 YAZ_PQF_Parser yaz_pqf_create(void)
763 YAZ_PQF_Parser p = (YAZ_PQF_Parser) xmalloc(sizeof(*p));
767 p->right_sep = "}\"";
768 p->escape_char = '@';
769 p->term_type = Z_Term_general;
774 void yaz_pqf_destroy(YAZ_PQF_Parser p)
779 Z_RPNQuery *yaz_pqf_parse(YAZ_PQF_Parser p, ODR o, const char *qbuf)
783 p->query_buf = p->query_ptr = qbuf;
785 return p_query_rpn_mk(o, p);
788 Z_AttributesPlusTerm *yaz_pqf_scan(YAZ_PQF_Parser p, ODR o,
789 Odr_oid **attributeSetP,
794 p->query_buf = p->query_ptr = qbuf;
796 return p_query_scan_mk(p, o, attributeSetP);
799 int yaz_pqf_error(YAZ_PQF_Parser p, const char **msg, size_t *off)
803 case YAZ_PQF_ERROR_NONE:
804 *msg = "no error"; break;
805 case YAZ_PQF_ERROR_EXTRA:
806 *msg = "extra token"; break;
807 case YAZ_PQF_ERROR_MISSING:
808 *msg = "missing token"; break;
809 case YAZ_PQF_ERROR_ATTSET:
810 *msg = "unknown attribute set"; break;
811 case YAZ_PQF_ERROR_TOOMANY:
812 *msg = "too many attributes"; break;
813 case YAZ_PQF_ERROR_BADATTR:
814 *msg = "bad attribute specification"; break;
815 case YAZ_PQF_ERROR_INTERNAL:
816 *msg = "internal error"; break;
817 case YAZ_PQF_ERROR_PROXIMITY:
818 *msg = "proximity error"; break;
819 case YAZ_PQF_ERROR_BAD_INTEGER:
820 *msg = "bad integer"; break;
822 *msg = "unknown error"; break;
824 *off = p->query_ptr - p->query_buf;
830 * c-file-style: "Stroustrup"
831 * indent-tabs-mode: nil
833 * vim: shiftwidth=4 tabstop=8 expandtab