2 * Copyright (c) 1995-2002, Index Data.
3 * See the file LICENSE for details.
5 * $Id: pquery.c,v 1.17 2002-09-02 13:59:07 adam Exp $
13 #include <yaz/proto.h>
15 #include <yaz/pquery.h>
17 static oid_value p_query_dfset = VAL_NONE;
19 struct yaz_pqf_parser {
20 const char *query_buf;
21 const char *query_ptr;
32 static Z_RPNStructure *rpn_structure (struct yaz_pqf_parser *li, ODR o,
34 int num_attr, int max_attr,
35 int *attr_list, char **attr_clist,
38 static enum oid_value query_oid_getvalbyname (struct yaz_pqf_parser *li)
45 memcpy (buf, li->lex_buf, li->lex_len);
46 buf[li->lex_len] = '\0';
47 value = oid_getvalbyname (buf);
51 static int compare_term (struct yaz_pqf_parser *li, const char *src,
54 size_t len=strlen(src);
56 if (li->lex_len == len+off && !memcmp (li->lex_buf+off, src, len-off))
61 static int query_token (struct yaz_pqf_parser *li)
64 const char *sep_match;
65 const char **qptr = &li->query_ptr;
72 if ((sep_match = strchr (li->left_sep, **qptr)))
74 sep_char = li->right_sep[sep_match - li->left_sep];
79 if (**qptr == li->escape_char && isdigit ((*qptr)[1]))
85 while (**qptr && **qptr != sep_char)
97 if (sep_char == ' ' &&
98 li->lex_len >= 1 && li->lex_buf[0] == li->escape_char)
100 if (compare_term (li, "and", 1))
102 if (compare_term (li, "or", 1))
104 if (compare_term (li, "not", 1))
106 if (compare_term (li, "attr", 1))
108 if (compare_term (li, "set", 1))
110 if (compare_term (li, "attrset", 1))
112 if (compare_term (li, "prox", 1))
114 if (compare_term (li, "term", 1))
120 static int lex (struct yaz_pqf_parser *li)
122 return li->query_look = query_token (li);
125 static int escape_string(char *out_buf, const char *in, int len)
130 if (*in == '\\' && len > 0)
156 sscanf (s, "%x", &n);
173 sscanf (s, "%o", &n);
185 return out - out_buf;
188 static int p_query_parse_attr(struct yaz_pqf_parser *li, ODR o,
189 int num_attr, int *attr_list,
190 char **attr_clist, oid_value *attr_set)
193 if (!(cp = strchr (li->lex_buf, '=')) ||
194 (size_t) (cp-li->lex_buf) > li->lex_len)
196 attr_set[num_attr] = query_oid_getvalbyname (li);
197 if (attr_set[num_attr] == VAL_NONE)
199 li->error = YAZ_PQF_ERROR_ATTSET;
204 li->error = YAZ_PQF_ERROR_MISSING;
207 if (!(cp = strchr (li->lex_buf, '=')))
209 li->error = YAZ_PQF_ERROR_BADATTR;
216 attr_set[num_attr] = attr_set[num_attr-1];
218 attr_set[num_attr] = VAL_NONE;
220 attr_list[2*num_attr] = atoi(li->lex_buf);
222 if (*cp >= '0' && *cp <= '9')
224 attr_list[2*num_attr+1] = atoi (cp);
225 attr_clist[num_attr] = 0;
229 int len = li->lex_len - (cp - li->lex_buf);
230 attr_list[2*num_attr+1] = 0;
231 attr_clist[num_attr] = (char *) odr_malloc (o, len+1);
232 len = escape_string(attr_clist[num_attr], cp, len);
233 attr_clist[num_attr][len] = '\0';
238 static Z_AttributesPlusTerm *rpn_term (struct yaz_pqf_parser *li, ODR o,
240 int num_attr, int *attr_list,
241 char **attr_clist, oid_value *attr_set)
243 Z_AttributesPlusTerm *zapt;
246 Z_AttributeElement **elements;
248 zapt = (Z_AttributesPlusTerm *)odr_malloc (o, sizeof(*zapt));
249 term_octet = (Odr_oct *)odr_malloc (o, sizeof(*term_octet));
250 term = (Z_Term *)odr_malloc (o, sizeof(*term));
253 elements = (Z_AttributeElement**)odr_nullval();
259 elements = (Z_AttributeElement**)
260 odr_malloc (o, num_attr * sizeof(*elements));
262 attr_tmp = (int *)odr_malloc (o, num_attr * 2 * sizeof(int));
263 memcpy (attr_tmp, attr_list, num_attr * 2 * sizeof(int));
264 for (i = num_attr; --i >= 0; )
267 for (j = i+1; j<num_attr; j++)
268 if (attr_tmp[2*j] == attr_tmp[2*i])
273 (Z_AttributeElement*)odr_malloc (o,sizeof(**elements));
274 elements[k]->attributeType = &attr_tmp[2*i];
275 elements[k]->attributeSet =
276 yaz_oidval_to_z3950oid(o, CLASS_ATTSET, attr_set[i]);
280 elements[k]->which = Z_AttributeValue_complex;
281 elements[k]->value.complex = (Z_ComplexAttribute *)
282 odr_malloc (o, sizeof(Z_ComplexAttribute));
283 elements[k]->value.complex->num_list = 1;
284 elements[k]->value.complex->list =
285 (Z_StringOrNumeric **)
286 odr_malloc (o, 1 * sizeof(Z_StringOrNumeric *));
287 elements[k]->value.complex->list[0] =
288 (Z_StringOrNumeric *)
289 odr_malloc (o, sizeof(Z_StringOrNumeric));
290 elements[k]->value.complex->list[0]->which =
291 Z_StringOrNumeric_string;
292 elements[k]->value.complex->list[0]->u.string =
294 elements[k]->value.complex->semanticAction = (int **)
296 elements[k]->value.complex->num_semanticAction = 0;
300 elements[k]->which = Z_AttributeValue_numeric;
301 elements[k]->value.numeric = &attr_tmp[2*i+1];
307 zapt->attributes = (Z_AttributeList *)
308 odr_malloc (o, sizeof(*zapt->attributes));
309 zapt->attributes->num_attributes = num_attr;
310 zapt->attributes->attributes = elements;
314 term_octet->buf = (unsigned char *)odr_malloc (o, 1 + li->lex_len);
315 term_octet->size = term_octet->len =
316 escape_string ((char *) (term_octet->buf), li->lex_buf, li->lex_len);
317 term_octet->buf[term_octet->size] = 0; /* null terminate */
319 switch (li->term_type)
322 term->which = Z_Term_general;
323 term->u.general = term_octet;
325 case Z_Term_characterString:
326 term->which = Z_Term_characterString;
327 term->u.characterString = term_octet->buf; /* null terminated above */
330 term->which = Z_Term_numeric;
331 term->u.numeric = odr_intdup (o, atoi(term_octet->buf));
334 term->which = Z_Term_null;
335 term->u.null = odr_nullval();
338 term->which = Z_Term_null;
339 term->u.null = odr_nullval();
345 static Z_Operand *rpn_simple (struct yaz_pqf_parser *li, ODR o, oid_proto proto,
346 int num_attr, int *attr_list, char **attr_clist,
351 zo = (Z_Operand *)odr_malloc (o, sizeof(*zo));
352 switch (li->query_look)
355 zo->which = Z_Operand_APT;
356 if (!(zo->u.attributesPlusTerm =
357 rpn_term (li, o, proto, num_attr, attr_list, attr_clist,
366 li->error = YAZ_PQF_ERROR_MISSING;
369 zo->which = Z_Operand_resultSetId;
370 zo->u.resultSetId = (char *)odr_malloc (o, li->lex_len+1);
371 memcpy (zo->u.resultSetId, li->lex_buf, li->lex_len);
372 zo->u.resultSetId[li->lex_len] = '\0';
376 /* we're only called if one of the above types are seens so
377 this shouldn't happen */
378 li->error = YAZ_PQF_ERROR_INTERNAL;
384 static Z_ProximityOperator *rpn_proximity (struct yaz_pqf_parser *li, ODR o)
386 Z_ProximityOperator *p = (Z_ProximityOperator *)odr_malloc (o, sizeof(*p));
390 li->error = YAZ_PQF_ERROR_MISSING;
393 if (*li->lex_buf == '1')
395 p->exclusion = (int *)odr_malloc (o, sizeof(*p->exclusion));
398 else if (*li->lex_buf == '0')
400 p->exclusion = (int *)odr_malloc (o, sizeof(*p->exclusion));
408 li->error = YAZ_PQF_ERROR_MISSING;
411 p->distance = (int *)odr_malloc (o, sizeof(*p->distance));
412 *p->distance = atoi (li->lex_buf);
416 li->error = YAZ_PQF_ERROR_MISSING;
419 p->ordered = (int *)odr_malloc (o, sizeof(*p->ordered));
420 *p->ordered = atoi (li->lex_buf);
424 li->error = YAZ_PQF_ERROR_MISSING;
427 p->relationType = (int *)odr_malloc (o, sizeof(*p->relationType));
428 *p->relationType = atoi (li->lex_buf);
432 li->error = YAZ_PQF_ERROR_MISSING;
435 if (*li->lex_buf == 'k')
437 else if (*li->lex_buf == 'p')
440 p->which = atoi (li->lex_buf);
444 li->error = YAZ_PQF_ERROR_MISSING;
447 p->which = Z_ProximityOperator_known;
448 p->u.known = (int *)odr_malloc (o, sizeof(*p->u.known));
449 *p->u.known = atoi (li->lex_buf);
453 static Z_Complex *rpn_complex (struct yaz_pqf_parser *li, ODR o, oid_proto proto,
454 int num_attr, int max_attr,
455 int *attr_list, char **attr_clist,
461 zc = (Z_Complex *)odr_malloc (o, sizeof(*zc));
462 zo = (Z_Operator *)odr_malloc (o, sizeof(*zo));
464 switch (li->query_look)
467 zo->which = Z_Operator_and;
468 zo->u.and_not = odr_nullval();
471 zo->which = Z_Operator_or;
472 zo->u.and_not = odr_nullval();
475 zo->which = Z_Operator_and_not;
476 zo->u.and_not = odr_nullval();
479 zo->which = Z_Operator_prox;
480 zo->u.prox = rpn_proximity (li, o);
485 /* we're only called if one of the above types are seens so
486 this shouldn't happen */
487 li->error = YAZ_PQF_ERROR_INTERNAL;
492 rpn_structure (li, o, proto, num_attr, max_attr, attr_list,
493 attr_clist, attr_set)))
496 rpn_structure (li, o, proto, num_attr, max_attr, attr_list,
497 attr_clist, attr_set)))
502 static void rpn_term_type (struct yaz_pqf_parser *li, ODR o)
506 if (compare_term (li, "general", 0))
507 li->term_type = Z_Term_general;
508 else if (compare_term (li, "numeric", 0))
509 li->term_type = Z_Term_numeric;
510 else if (compare_term (li, "string", 0))
511 li->term_type = Z_Term_characterString;
512 else if (compare_term (li, "oid", 0))
513 li->term_type = Z_Term_oid;
514 else if (compare_term (li, "datetime", 0))
515 li->term_type = Z_Term_dateTime;
516 else if (compare_term (li, "null", 0))
517 li->term_type = Z_Term_null;
521 static Z_RPNStructure *rpn_structure (struct yaz_pqf_parser *li, ODR o,
523 int num_attr, int max_attr,
530 sz = (Z_RPNStructure *)odr_malloc (o, sizeof(*sz));
531 switch (li->query_look)
537 sz->which = Z_RPNStructure_complex;
538 if (!(sz->u.complex =
539 rpn_complex (li, o, proto, num_attr, max_attr, attr_list,
540 attr_clist, attr_set)))
545 sz->which = Z_RPNStructure_simple;
547 rpn_simple (li, o, proto, num_attr, attr_list,
548 attr_clist, attr_set)))
555 li->error = YAZ_PQF_ERROR_MISSING;
558 if (num_attr >= max_attr)
560 li->error = YAZ_PQF_ERROR_TOOMANY;
563 if (!p_query_parse_attr(li, o, num_attr, attr_list,
564 attr_clist, attr_set))
569 rpn_structure (li, o, proto, num_attr, max_attr, attr_list,
570 attr_clist, attr_set);
573 rpn_term_type (li, o);
575 rpn_structure (li, o, proto, num_attr, max_attr, attr_list,
576 attr_clist, attr_set);
577 case 0: /* operator/operand expected! */
578 li->error = YAZ_PQF_ERROR_MISSING;
584 Z_RPNQuery *p_query_rpn_mk (ODR o, struct yaz_pqf_parser *li, oid_proto proto,
588 int attr_array[1024];
589 char *attr_clist[512];
590 oid_value attr_set[512];
591 oid_value topSet = VAL_NONE;
593 zq = (Z_RPNQuery *)odr_malloc (o, sizeof(*zq));
595 if (li->query_look == 'r')
598 topSet = query_oid_getvalbyname (li);
599 if (topSet == VAL_NONE)
601 li->error = YAZ_PQF_ERROR_ATTSET;
607 if (topSet == VAL_NONE)
608 topSet = p_query_dfset;
609 if (topSet == VAL_NONE)
612 zq->attributeSetId = yaz_oidval_to_z3950oid(o, CLASS_ATTSET, topSet);
614 if (!zq->attributeSetId)
616 li->error = YAZ_PQF_ERROR_ATTSET;
620 if (!(zq->RPNStructure = rpn_structure (li, o, proto, 0, 512,
621 attr_array, attr_clist, attr_set)))
625 li->error = YAZ_PQF_ERROR_EXTRA;
631 Z_RPNQuery *p_query_rpn (ODR o, oid_proto proto,
634 struct yaz_pqf_parser li;
638 li.right_sep = "}\"";
639 li.escape_char = '@';
640 li.term_type = Z_Term_general;
641 li.query_buf = li.query_ptr = qbuf;
643 return p_query_rpn_mk (o, &li, proto, qbuf);
647 Z_AttributesPlusTerm *p_query_scan_mk (struct yaz_pqf_parser *li,
648 ODR o, oid_proto proto,
649 Odr_oid **attributeSetP,
653 char *attr_clist[512];
654 oid_value attr_set[512];
657 oid_value topSet = VAL_NONE;
658 Z_AttributesPlusTerm *apt;
661 if (li->query_look == 'r')
664 topSet = query_oid_getvalbyname (li);
668 if (topSet == VAL_NONE)
669 topSet = p_query_dfset;
670 if (topSet == VAL_NONE)
673 *attributeSetP = yaz_oidval_to_z3950oid (o, CLASS_ATTSET, topSet);
677 if (li->query_look == 'l')
682 li->error = YAZ_PQF_ERROR_MISSING;
685 if (num_attr >= max_attr)
687 li->error = YAZ_PQF_ERROR_TOOMANY;
690 if (!p_query_parse_attr(li, o, num_attr, attr_list,
691 attr_clist, attr_set))
696 else if (li->query_look == 'y')
699 rpn_term_type (li, o);
706 li->error = YAZ_PQF_ERROR_MISSING;
709 apt = rpn_term (li, o, proto, num_attr, attr_list, attr_clist, attr_set);
713 if (li->query_look != 0)
715 li->error = YAZ_PQF_ERROR_EXTRA;
721 Z_AttributesPlusTerm *p_query_scan (ODR o, oid_proto proto,
722 Odr_oid **attributeSetP,
725 struct yaz_pqf_parser li;
729 li.right_sep = "}\"";
730 li.escape_char = '@';
731 li.term_type = Z_Term_general;
732 li.query_buf = li.query_ptr = qbuf;
735 return p_query_scan_mk (&li, o, proto, attributeSetP, qbuf);
738 int p_query_attset (const char *arg)
740 p_query_dfset = oid_getvalbyname (arg);
741 return (p_query_dfset == VAL_NONE) ? -1 : 0;
744 YAZ_PQF_Parser yaz_pqf_create (void)
746 YAZ_PQF_Parser p = xmalloc (sizeof(*p));
750 p->right_sep = "}\"";
751 p->escape_char = '@';
752 p->term_type = Z_Term_general;
757 void yaz_pqf_destroy (YAZ_PQF_Parser p)
762 Z_RPNQuery *yaz_pqf_parse (YAZ_PQF_Parser p, ODR o, const char *qbuf)
766 p->query_buf = p->query_ptr = qbuf;
768 return p_query_rpn_mk (o, p, PROTO_Z3950, qbuf);
771 Z_AttributesPlusTerm *yaz_pqf_scan (YAZ_PQF_Parser p, ODR o,
772 Odr_oid **attributeSetP,
777 p->query_buf = p->query_ptr = qbuf;
779 return p_query_scan_mk (p, o, PROTO_Z3950, attributeSetP, qbuf);
782 int yaz_pqf_error (YAZ_PQF_Parser p, const char **msg, size_t *off)
786 case YAZ_PQF_ERROR_NONE:
787 *msg = "no error"; break;
788 case YAZ_PQF_ERROR_EXTRA:
789 *msg = "extra token"; break;
790 case YAZ_PQF_ERROR_MISSING:
791 *msg = "missing token"; break;
792 case YAZ_PQF_ERROR_ATTSET:
793 *msg = "unknown attribute set"; break;
794 case YAZ_PQF_ERROR_TOOMANY:
795 *msg = "too many attributes"; break;
796 case YAZ_PQF_ERROR_BADATTR:
797 *msg = "bad attribute specification"; break;
798 case YAZ_PQF_ERROR_INTERNAL:
799 *msg = "internal error"; break;
801 *msg = "unknown error"; break;
803 *off = p->query_ptr - p->query_buf;