X-Git-Url: http://git.indexdata.com/?a=blobdiff_plain;f=zutil%2Fpquery.c;h=284055736593af5fac9fe62c8e2dbc2c740c4db7;hb=94c77ca831e55b3dee31184347f25dd0583085d2;hp=6f35004c51fd304b7b83ae389c26d0d3b8783394;hpb=e347736dafbae4bbd1eec56e3577353662aee332;p=yaz-moved-to-github.git diff --git a/zutil/pquery.c b/zutil/pquery.c index 6f35004..2840557 100644 --- a/zutil/pquery.c +++ b/zutil/pquery.c @@ -1,95 +1,14 @@ /* - * Copyright (c) 1995-1998, Index Data. + * Copyright (c) 1995-2003, Index Data. * See the file LICENSE for details. - * Sebastian Hammer, Adam Dickmeiss - * - * $Log: pquery.c,v $ - * Revision 1.3 1999-12-20 15:20:13 adam - * Implemented ccl_pquery to convert from CCL tree to prefix query. - * - * Revision 1.2 1999/11/30 13:47:12 adam - * Improved installation. Moved header files to include/yaz. - * - * Revision 1.1 1999/06/08 10:10:16 adam - * New sub directory zutil. Moved YAZ Compiler to be part of YAZ tree. - * - * Revision 1.22 1999/04/20 09:56:49 adam - * Added 'name' paramter to encoder/decoder routines (typedef Odr_fun). - * Modified all encoders/decoders to reflect this change. - * - * Revision 1.21 1998/10/13 16:03:37 adam - * Better checking for invalid OID's in p_query_rpn. - * - * Revision 1.20 1998/03/31 15:13:20 adam - * Development towards compiled ASN.1. - * - * Revision 1.19 1998/03/05 08:09:03 adam - * Minor change to make C++ happy. - * - * Revision 1.18 1998/02/11 11:53:36 adam - * Changed code so that it compiles as C++. - * - * Revision 1.17 1997/11/24 11:33:57 adam - * Using function odr_nullval() instead of global ODR_NULLVAL when - * appropriate. - * - * Revision 1.16 1997/09/29 13:19:00 adam - * Added function, oid_ent_to_oid, to replace the function - * oid_getoidbyent, which is not thread safe. - * - * Revision 1.15 1997/09/29 07:13:43 adam - * Changed type of a few variables to avoid warnings. - * - * Revision 1.14 1997/09/22 12:33:41 adam - * Fixed bug introduced by previous commit. - * - * Revision 1.13 1997/09/17 12:10:42 adam - * YAZ version 1.4. - * - * Revision 1.12 1997/09/01 08:54:13 adam - * New windows NT/95 port using MSV5.0. Made prefix query handling - * thread safe. The function options ignores empty arguments when met. - * - * Revision 1.11 1996/11/11 13:15:29 adam - * Added proximity operator. - * - * Revision 1.10 1996/08/12 14:10:35 adam - * New function p_query_attset to define default attribute set. - * - * Revision 1.9 1996/03/15 11:03:46 adam - * Attribute set can be set globally for a query with the @attrset - * operator. The @attr operator has an optional attribute-set specifier - * that sets the attribute set locally. - * - * Revision 1.8 1996/01/02 11:46:56 quinn - * Changed 'operator' to 'roperator' to avoid C++ conflict. - * - * Revision 1.7 1995/09/29 17:12:36 quinn - * Smallish - * - * Revision 1.6 1995/09/27 15:03:03 quinn - * Modified function heads & prototypes. - * - * Revision 1.5 1995/06/15 12:31:02 quinn - * *** empty log message *** - * - * Revision 1.4 1995/06/15 07:45:19 quinn - * Moving to v3. - * - * Revision 1.3 1995/06/14 11:06:35 adam - * Bug fix: Attributes wasn't interpreted correctly! - * - * Revision 1.2 1995/05/26 08:56:11 adam - * New function: p_query_scan. - * - * Revision 1.1 1995/05/22 15:31:49 adam - * New function, p_query_rpn, to convert from prefix (ascii) to rpn (asn). * + * $Id: pquery.c,v 1.20 2003-01-06 08:20:29 adam Exp $ */ #include #include #include +#include #include #include @@ -97,8 +16,9 @@ static oid_value p_query_dfset = VAL_NONE; -struct lex_info { +struct yaz_pqf_parser { const char *query_buf; + const char *query_ptr; const char *lex_buf; size_t lex_len; int query_look; @@ -106,13 +26,17 @@ struct lex_info { char *right_sep; int escape_char; int term_type; + int external_type; + int error; }; -static Z_RPNStructure *rpn_structure (struct lex_info *li, ODR o, oid_proto, +static Z_RPNStructure *rpn_structure (struct yaz_pqf_parser *li, ODR o, + oid_proto, int num_attr, int max_attr, - int *attr_list, oid_value *attr_set); + int *attr_list, char **attr_clist, + oid_value *attr_set); -static enum oid_value query_oid_getvalbyname (struct lex_info *li) +static enum oid_value query_oid_getvalbyname (struct yaz_pqf_parser *li) { enum oid_value value; char buf[32]; @@ -125,19 +49,21 @@ static enum oid_value query_oid_getvalbyname (struct lex_info *li) return value; } -static int compare_term (struct lex_info *li, const char *src, size_t off) +static int compare_term (struct yaz_pqf_parser *li, const char *src, + size_t off) { size_t len=strlen(src); - + if (li->lex_len == len+off && !memcmp (li->lex_buf+off, src, len-off)) return 1; return 0; } -static int query_token (struct lex_info *li) +static int query_token (struct yaz_pqf_parser *li) { + int sep_char = ' '; const char *sep_match; - const char **qptr = &li->query_buf; + const char **qptr = &li->query_ptr; while (**qptr == ' ') (*qptr)++; @@ -146,28 +72,31 @@ static int query_token (struct lex_info *li) li->lex_len = 0; if ((sep_match = strchr (li->left_sep, **qptr))) { - int sep_index = sep_match - li->left_sep; - + sep_char = li->right_sep[sep_match - li->left_sep]; ++(*qptr); - li->lex_buf = *qptr; - while (**qptr && **qptr != li->right_sep[sep_index]) - { - ++(li->lex_len); - ++(*qptr); - } - if (**qptr) - ++(*qptr); } - else + li->lex_buf = *qptr; + + if (**qptr == li->escape_char && isdigit ((*qptr)[1])) { - li->lex_buf = *qptr; - while (**qptr && **qptr != ' ') - { - ++(li->lex_len); - ++(*qptr); - } + ++(li->lex_len); + ++(*qptr); + return 'l'; + } + while (**qptr && **qptr != sep_char) + { + if (**qptr == '\\') + { + ++(li->lex_len); + ++(*qptr); + } + ++(li->lex_len); + ++(*qptr); } - if (li->lex_len >= 1 && li->lex_buf[0] == li->escape_char) + if (**qptr) + ++(*qptr); + if (sep_char == ' ' && + li->lex_len >= 1 && li->lex_buf[0] == li->escape_char) { if (compare_term (li, "and", 1)) return 'a'; @@ -189,15 +118,128 @@ static int query_token (struct lex_info *li) return 't'; } -static int lex (struct lex_info *li) +static int lex (struct yaz_pqf_parser *li) { return li->query_look = query_token (li); } -static Z_AttributesPlusTerm *rpn_term (struct lex_info *li, ODR o, +static int escape_string(char *out_buf, const char *in, int len) +{ + + char *out = out_buf; + while (--len >= 0) + if (*in == '\\' && len > 0) + { + --len; + switch (*++in) + { + case 't': + *out++ = '\t'; + break; + case 'n': + *out++ = '\n'; + break; + case 'r': + *out++ = '\r'; + break; + case 'f': + *out++ = '\f'; + break; + case 'x': + if (len > 1) + { + char s[4]; + int n = 0; + s[0] = *++in; + s[1] = *++in; + s[2] = '\0'; + len = len - 2; + sscanf (s, "%x", &n); + *out++ = n; + } + break; + case '0': + case '1': + case '2': + case '3': + if (len > 1) + { + char s[4]; + int n = 0; + s[0] = *in; + s[1] = *++in; + s[2] = *++in; + s[3] = '\0'; + len = len - 2; + sscanf (s, "%o", &n); + *out++ = n; + } + break; + default: + *out++ = *in; + break; + } + in++; + } + else + *out++ = *in++; + return out - out_buf; +} + +static int p_query_parse_attr(struct yaz_pqf_parser *li, ODR o, + int num_attr, int *attr_list, + char **attr_clist, oid_value *attr_set) +{ + const char *cp; + if (!(cp = strchr (li->lex_buf, '=')) || + (size_t) (cp-li->lex_buf) > li->lex_len) + { + attr_set[num_attr] = query_oid_getvalbyname (li); + if (attr_set[num_attr] == VAL_NONE) + { + li->error = YAZ_PQF_ERROR_ATTSET; + return 0; + } + if (!lex (li)) + { + li->error = YAZ_PQF_ERROR_MISSING; + return 0; + } + if (!(cp = strchr (li->lex_buf, '='))) + { + li->error = YAZ_PQF_ERROR_BADATTR; + return 0; + } + } + else + { + if (num_attr > 0) + attr_set[num_attr] = attr_set[num_attr-1]; + else + attr_set[num_attr] = VAL_NONE; + } + attr_list[2*num_attr] = atoi(li->lex_buf); + cp++; + if (*cp >= '0' && *cp <= '9') + { + attr_list[2*num_attr+1] = atoi (cp); + attr_clist[num_attr] = 0; + } + else + { + int len = li->lex_len - (cp - li->lex_buf); + attr_list[2*num_attr+1] = 0; + attr_clist[num_attr] = (char *) odr_malloc (o, len+1); + len = escape_string(attr_clist[num_attr], cp, len); + attr_clist[num_attr][len] = '\0'; + } + return 1; +} + +static Z_AttributesPlusTerm *rpn_term (struct yaz_pqf_parser *li, ODR o, oid_proto proto, int num_attr, int *attr_list, - oid_value *attr_set) + char **attr_clist, oid_value *attr_set) { Z_AttributesPlusTerm *zapt; Odr_oct *term_octet; @@ -212,7 +254,7 @@ static Z_AttributesPlusTerm *rpn_term (struct lex_info *li, ODR o, elements = (Z_AttributeElement**)odr_nullval(); else { - int i; + int i, k = 0; int *attr_tmp; elements = (Z_AttributeElement**) @@ -220,50 +262,94 @@ static Z_AttributesPlusTerm *rpn_term (struct lex_info *li, ODR o, attr_tmp = (int *)odr_malloc (o, num_attr * 2 * sizeof(int)); memcpy (attr_tmp, attr_list, num_attr * 2 * sizeof(int)); - for (i = 0; i < num_attr; i++) + for (i = num_attr; --i >= 0; ) { - elements[i] = + int j; + for (j = i+1; jattributeType = &attr_tmp[2*i]; - if (attr_set[i] == VAL_NONE) - elements[i]->attributeSet = 0; - else - { - oident attrid; - int oid[OID_SIZE]; - - attrid.proto = PROTO_Z3950; - attrid.oclass = CLASS_ATTSET; - attrid.value = attr_set[i]; - - elements[i]->attributeSet = - odr_oiddup (o, oid_ent_to_oid (&attrid, oid)); - } - elements[i]->which = Z_AttributeValue_numeric; - elements[i]->value.numeric = &attr_tmp[2*i+1]; + elements[k]->attributeType = &attr_tmp[2*i]; + elements[k]->attributeSet = + yaz_oidval_to_z3950oid(o, CLASS_ATTSET, attr_set[i]); + + if (attr_clist[i]) + { + elements[k]->which = Z_AttributeValue_complex; + elements[k]->value.complex = (Z_ComplexAttribute *) + odr_malloc (o, sizeof(Z_ComplexAttribute)); + elements[k]->value.complex->num_list = 1; + elements[k]->value.complex->list = + (Z_StringOrNumeric **) + odr_malloc (o, 1 * sizeof(Z_StringOrNumeric *)); + elements[k]->value.complex->list[0] = + (Z_StringOrNumeric *) + odr_malloc (o, sizeof(Z_StringOrNumeric)); + elements[k]->value.complex->list[0]->which = + Z_StringOrNumeric_string; + elements[k]->value.complex->list[0]->u.string = + attr_clist[i]; + elements[k]->value.complex->semanticAction = (int **) + odr_nullval(); + elements[k]->value.complex->num_semanticAction = 0; + } + else + { + elements[k]->which = Z_AttributeValue_numeric; + elements[k]->value.numeric = &attr_tmp[2*i+1]; + } + k++; } + num_attr = k; } -#ifdef ASN_COMPILED zapt->attributes = (Z_AttributeList *) odr_malloc (o, sizeof(*zapt->attributes)); zapt->attributes->num_attributes = num_attr; zapt->attributes->attributes = elements; -#else - zapt->num_attributes = num_attr; - zapt->attributeList = elements; -#endif zapt->term = term; - term->which = Z_Term_general; - term->u.general = term_octet; - term_octet->buf = (unsigned char *)odr_malloc (o, li->lex_len); - term_octet->size = term_octet->len = li->lex_len; - memcpy (term_octet->buf, li->lex_buf, li->lex_len); + + term_octet->buf = (unsigned char *)odr_malloc (o, 1 + li->lex_len); + term_octet->size = term_octet->len = + escape_string ((char *) (term_octet->buf), li->lex_buf, li->lex_len); + term_octet->buf[term_octet->size] = 0; /* null terminate */ + + switch (li->term_type) + { + case Z_Term_general: + term->which = Z_Term_general; + term->u.general = term_octet; + break; + case Z_Term_characterString: + term->which = Z_Term_characterString; + term->u.characterString = (char*) term_octet->buf; + /* null terminated above */ + break; + case Z_Term_numeric: + term->which = Z_Term_numeric; + term->u.numeric = odr_intdup (o, atoi((char*) (term_octet->buf))); + break; + case Z_Term_null: + term->which = Z_Term_null; + term->u.null = odr_nullval(); + break; + case Z_Term_external: + term->which = Z_Term_external; + term->u.external = 0; + break; + default: + term->which = Z_Term_null; + term->u.null = odr_nullval(); + break; + } return zapt; } -static Z_Operand *rpn_simple (struct lex_info *li, ODR o, oid_proto proto, - int num_attr, int *attr_list, +static Z_Operand *rpn_simple (struct yaz_pqf_parser *li, ODR o, oid_proto proto, + int num_attr, int *attr_list, char **attr_clist, oid_value *attr_set) { Z_Operand *zo; @@ -274,14 +360,18 @@ static Z_Operand *rpn_simple (struct lex_info *li, ODR o, oid_proto proto, case 't': zo->which = Z_Operand_APT; if (!(zo->u.attributesPlusTerm = - rpn_term (li, o, proto, num_attr, attr_list, attr_set))) - return NULL; + rpn_term (li, o, proto, num_attr, attr_list, attr_clist, + attr_set))) + return 0; lex (li); break; case 's': lex (li); if (!li->query_look) - return NULL; + { + li->error = YAZ_PQF_ERROR_MISSING; + return 0; + } zo->which = Z_Operand_resultSetId; zo->u.resultSetId = (char *)odr_malloc (o, li->lex_len+1); memcpy (zo->u.resultSetId, li->lex_buf, li->lex_len); @@ -289,17 +379,23 @@ static Z_Operand *rpn_simple (struct lex_info *li, ODR o, oid_proto proto, lex (li); break; default: - return NULL; + /* we're only called if one of the above types are seens so + this shouldn't happen */ + li->error = YAZ_PQF_ERROR_INTERNAL; + return 0; } return zo; } -static Z_ProximityOperator *rpn_proximity (struct lex_info *li, ODR o) +static Z_ProximityOperator *rpn_proximity (struct yaz_pqf_parser *li, ODR o) { Z_ProximityOperator *p = (Z_ProximityOperator *)odr_malloc (o, sizeof(*p)); if (!lex (li)) + { + li->error = YAZ_PQF_ERROR_MISSING; return NULL; + } if (*li->lex_buf == '1') { p->exclusion = (int *)odr_malloc (o, sizeof(*p->exclusion)); @@ -314,22 +410,34 @@ static Z_ProximityOperator *rpn_proximity (struct lex_info *li, ODR o) p->exclusion = NULL; if (!lex (li)) + { + li->error = YAZ_PQF_ERROR_MISSING; return NULL; + } p->distance = (int *)odr_malloc (o, sizeof(*p->distance)); *p->distance = atoi (li->lex_buf); if (!lex (li)) + { + li->error = YAZ_PQF_ERROR_MISSING; return NULL; + } p->ordered = (int *)odr_malloc (o, sizeof(*p->ordered)); *p->ordered = atoi (li->lex_buf); if (!lex (li)) + { + li->error = YAZ_PQF_ERROR_MISSING; return NULL; + } p->relationType = (int *)odr_malloc (o, sizeof(*p->relationType)); *p->relationType = atoi (li->lex_buf); if (!lex (li)) + { + li->error = YAZ_PQF_ERROR_MISSING; return NULL; + } if (*li->lex_buf == 'k') p->which = 0; else if (*li->lex_buf == 'p') @@ -338,21 +446,20 @@ static Z_ProximityOperator *rpn_proximity (struct lex_info *li, ODR o) p->which = atoi (li->lex_buf); if (!lex (li)) + { + li->error = YAZ_PQF_ERROR_MISSING; return NULL; -#ifdef ASN_COMPILED + } p->which = Z_ProximityOperator_known; p->u.known = (int *)odr_malloc (o, sizeof(*p->u.known)); *p->u.known = atoi (li->lex_buf); -#else - p->proximityUnitCode = (int *)odr_malloc (o, sizeof(*p->proximityUnitCode)); - *p->proximityUnitCode = atoi (li->lex_buf); -#endif return p; } -static Z_Complex *rpn_complex (struct lex_info *li, ODR o, oid_proto proto, +static Z_Complex *rpn_complex (struct yaz_pqf_parser *li, ODR o, oid_proto proto, int num_attr, int max_attr, - int *attr_list, oid_value *attr_set) + int *attr_list, char **attr_clist, + oid_value *attr_set) { Z_Complex *zc; Z_Operator *zo; @@ -364,15 +471,15 @@ static Z_Complex *rpn_complex (struct lex_info *li, ODR o, oid_proto proto, { case 'a': zo->which = Z_Operator_and; - zo->u.and = odr_nullval(); + zo->u.and_not = odr_nullval(); break; case 'o': zo->which = Z_Operator_or; - zo->u.and = odr_nullval(); + zo->u.and_not = odr_nullval(); break; case 'n': zo->which = Z_Operator_and_not; - zo->u.and = odr_nullval(); + zo->u.and_not = odr_nullval(); break; case 'p': zo->which = Z_Operator_prox; @@ -381,28 +488,56 @@ static Z_Complex *rpn_complex (struct lex_info *li, ODR o, oid_proto proto, return NULL; break; default: + /* we're only called if one of the above types are seens so + this shouldn't happen */ + li->error = YAZ_PQF_ERROR_INTERNAL; return NULL; } lex (li); if (!(zc->s1 = rpn_structure (li, o, proto, num_attr, max_attr, attr_list, - attr_set))) + attr_clist, attr_set))) return NULL; if (!(zc->s2 = rpn_structure (li, o, proto, num_attr, max_attr, attr_list, - attr_set))) + attr_clist, attr_set))) return NULL; return zc; } -static Z_RPNStructure *rpn_structure (struct lex_info *li, ODR o, +static void rpn_term_type (struct yaz_pqf_parser *li, ODR o) +{ + if (!li->query_look) + return ; + if (compare_term (li, "general", 0)) + li->term_type = Z_Term_general; + else if (compare_term (li, "numeric", 0)) + li->term_type = Z_Term_numeric; + else if (compare_term (li, "string", 0)) + li->term_type = Z_Term_characterString; + else if (compare_term (li, "oid", 0)) + li->term_type = Z_Term_oid; + else if (compare_term (li, "datetime", 0)) + li->term_type = Z_Term_dateTime; + else if (compare_term (li, "null", 0)) + li->term_type = Z_Term_null; + else if (compare_term(li, "range", 0)) + { + /* prepare for external: range search .. */ + li->term_type = Z_Term_external; + li->external_type = VAL_MULTISRCH2; + } + lex (li); +} + +static Z_RPNStructure *rpn_structure (struct yaz_pqf_parser *li, ODR o, oid_proto proto, int num_attr, int max_attr, - int *attr_list, oid_value *attr_set) + int *attr_list, + char **attr_clist, + oid_value *attr_set) { Z_RPNStructure *sz; - const char *cp; - int i, attrtype; sz = (Z_RPNStructure *)odr_malloc (o, sizeof(*sz)); switch (li->query_look) @@ -414,7 +549,7 @@ static Z_RPNStructure *rpn_structure (struct lex_info *li, ODR o, sz->which = Z_RPNStructure_complex; if (!(sz->u.complex = rpn_complex (li, o, proto, num_attr, max_attr, attr_list, - attr_set))) + attr_clist, attr_set))) return NULL; break; case 't': @@ -422,85 +557,50 @@ static Z_RPNStructure *rpn_structure (struct lex_info *li, ODR o, sz->which = Z_RPNStructure_simple; if (!(sz->u.simple = rpn_simple (li, o, proto, num_attr, attr_list, - attr_set))) + attr_clist, attr_set))) return NULL; break; case 'l': lex (li); if (!li->query_look) - return NULL; - if (num_attr >= max_attr) - return NULL; - if (!(cp = strchr (li->lex_buf, '=')) || - (size_t) (cp-li->lex_buf) > li->lex_len) { - attr_set[num_attr] = query_oid_getvalbyname (li); - if (attr_set[num_attr] == VAL_NONE) - return NULL; - lex (li); - - if (!(cp = strchr (li->lex_buf, '='))) - return NULL; + li->error = YAZ_PQF_ERROR_MISSING; + return 0; } - else + if (num_attr >= max_attr) { - if (num_attr > 0) - attr_set[num_attr] = attr_set[num_attr-1]; - else - attr_set[num_attr] = VAL_NONE; + li->error = YAZ_PQF_ERROR_TOOMANY; + return 0; } - attrtype = atoi (li->lex_buf); - for (i = 0; i < num_attr; i++) - if (attrtype == attr_list[2*i]) - { - attr_list[2*i+1] = atoi (cp+1); - break; - } - if (i == num_attr) - { - attr_list[2*num_attr] = attrtype; - attr_list[2*num_attr+1] = atoi (cp+1); - num_attr++; - } + if (!p_query_parse_attr(li, o, num_attr, attr_list, + attr_clist, attr_set)) + return 0; + num_attr++; lex (li); return rpn_structure (li, o, proto, num_attr, max_attr, attr_list, - attr_set); + attr_clist, attr_set); case 'y': lex (li); - if (!li->query_look) - return NULL; - if (compare_term (li, "general", 0)) - li->term_type = Z_Term_general; - else if (compare_term (li, "numeric", 0)) - li->term_type = Z_Term_numeric; - else if (compare_term (li, "string", 0)) - li->term_type = Z_Term_characterString; - else if (compare_term (li, "oid", 0)) - li->term_type = Z_Term_oid; - else if (compare_term (li, "datetime", 0)) - li->term_type = Z_Term_dateTime; - else if (compare_term (li, "null", 0)) - li->term_type = Z_Term_null; - lex (li); + rpn_term_type (li, o); return rpn_structure (li, o, proto, num_attr, max_attr, attr_list, - attr_set); + attr_clist, attr_set); case 0: /* operator/operand expected! */ - return NULL; + li->error = YAZ_PQF_ERROR_MISSING; + return 0; } return sz; } -Z_RPNQuery *p_query_rpn_mk (ODR o, struct lex_info *li, oid_proto proto, +Z_RPNQuery *p_query_rpn_mk (ODR o, struct yaz_pqf_parser *li, oid_proto proto, const char *qbuf) { Z_RPNQuery *zq; int attr_array[1024]; + char *attr_clist[512]; oid_value attr_set[512]; oid_value topSet = VAL_NONE; - oident oset; - int oid[OID_SIZE]; zq = (Z_RPNQuery *)odr_malloc (o, sizeof(*zq)); lex (li); @@ -509,7 +609,10 @@ Z_RPNQuery *p_query_rpn_mk (ODR o, struct lex_info *li, oid_proto proto, lex (li); topSet = query_oid_getvalbyname (li); if (topSet == VAL_NONE) + { + li->error = YAZ_PQF_ERROR_ATTSET; return NULL; + } lex (li); } @@ -517,46 +620,54 @@ Z_RPNQuery *p_query_rpn_mk (ODR o, struct lex_info *li, oid_proto proto, topSet = p_query_dfset; if (topSet == VAL_NONE) topSet = VAL_BIB1; - oset.proto = proto; - oset.oclass = CLASS_ATTSET; - oset.value = topSet; - if (!oid_ent_to_oid (&oset, oid)) - return NULL; - zq->attributeSetId = odr_oiddup (o, oid); + zq->attributeSetId = yaz_oidval_to_z3950oid(o, CLASS_ATTSET, topSet); + + if (!zq->attributeSetId) + { + li->error = YAZ_PQF_ERROR_ATTSET; + return 0; + } if (!(zq->RPNStructure = rpn_structure (li, o, proto, 0, 512, - attr_array, attr_set))) - return NULL; + attr_array, attr_clist, attr_set))) + return 0; + if (li->query_look) + { + li->error = YAZ_PQF_ERROR_EXTRA; + return 0; + } return zq; } Z_RPNQuery *p_query_rpn (ODR o, oid_proto proto, const char *qbuf) { - struct lex_info li; - + struct yaz_pqf_parser li; + + li.error = 0; li.left_sep = "{\""; li.right_sep = "}\""; li.escape_char = '@'; li.term_type = Z_Term_general; - li.query_buf = qbuf; + li.query_buf = li.query_ptr = qbuf; + li.lex_buf = 0; return p_query_rpn_mk (o, &li, proto, qbuf); } -Z_AttributesPlusTerm *p_query_scan_mk (struct lex_info *li, + +Z_AttributesPlusTerm *p_query_scan_mk (struct yaz_pqf_parser *li, ODR o, oid_proto proto, Odr_oid **attributeSetP, const char *qbuf) { int attr_list[1024]; + char *attr_clist[512]; oid_value attr_set[512]; int num_attr = 0; int max_attr = 512; - const char *cp; oid_value topSet = VAL_NONE; - oident oset; - int oid[OID_SIZE]; + Z_AttributesPlusTerm *apt; lex (li); if (li->query_look == 'r') @@ -570,57 +681,68 @@ Z_AttributesPlusTerm *p_query_scan_mk (struct lex_info *li, topSet = p_query_dfset; if (topSet == VAL_NONE) topSet = VAL_BIB1; - oset.proto = proto; - oset.oclass = CLASS_ATTSET; - oset.value = topSet; - *attributeSetP = odr_oiddup (o, oid_ent_to_oid (&oset, oid)); + *attributeSetP = yaz_oidval_to_z3950oid (o, CLASS_ATTSET, topSet); - while (li->query_look == 'l') + while (1) { - lex (li); - if (!li->query_look) - return NULL; - if (num_attr >= max_attr) - return NULL; - - if (!(cp = strchr (li->lex_buf, '=')) || - (size_t) (cp-li->lex_buf) > li->lex_len) + if (li->query_look == 'l') { - attr_set[num_attr] = query_oid_getvalbyname (li); lex (li); - - if (!(cp = strchr (li->lex_buf, '='))) - return NULL; + if (!li->query_look) + { + li->error = YAZ_PQF_ERROR_MISSING; + return 0; + } + if (num_attr >= max_attr) + { + li->error = YAZ_PQF_ERROR_TOOMANY; + return 0; + } + if (!p_query_parse_attr(li, o, num_attr, attr_list, + attr_clist, attr_set)) + return 0; + num_attr++; + lex (li); } - else + else if (li->query_look == 'y') { - if (num_attr > 0) - attr_set[num_attr] = attr_set[num_attr-1]; - else - attr_set[num_attr] = VAL_NONE; + lex (li); + rpn_term_type (li, o); } - attr_list[2*num_attr] = atoi (li->lex_buf); - attr_list[2*num_attr+1] = atoi (cp+1); - num_attr++; - lex (li); + else + break; } if (!li->query_look) - return NULL; - return rpn_term (li, o, proto, num_attr, attr_list, attr_set); + { + li->error = YAZ_PQF_ERROR_MISSING; + return 0; + } + apt = rpn_term (li, o, proto, num_attr, attr_list, attr_clist, attr_set); + + lex (li); + + if (li->query_look != 0) + { + li->error = YAZ_PQF_ERROR_EXTRA; + return 0; + } + return apt; } Z_AttributesPlusTerm *p_query_scan (ODR o, oid_proto proto, Odr_oid **attributeSetP, const char *qbuf) { - struct lex_info li; + struct yaz_pqf_parser li; + li.error = 0; li.left_sep = "{\""; li.right_sep = "}\""; li.escape_char = '@'; li.term_type = Z_Term_general; - li.query_buf = qbuf; + li.query_buf = li.query_ptr = qbuf; + li.lex_buf = 0; return p_query_scan_mk (&li, o, proto, attributeSetP, qbuf); } @@ -631,3 +753,65 @@ int p_query_attset (const char *arg) return (p_query_dfset == VAL_NONE) ? -1 : 0; } +YAZ_PQF_Parser yaz_pqf_create (void) +{ + YAZ_PQF_Parser p = (YAZ_PQF_Parser) xmalloc (sizeof(*p)); + + p->error = 0; + p->left_sep = "{\""; + p->right_sep = "}\""; + p->escape_char = '@'; + p->term_type = Z_Term_general; + + return p; +} + +void yaz_pqf_destroy (YAZ_PQF_Parser p) +{ + xfree (p); +} + +Z_RPNQuery *yaz_pqf_parse (YAZ_PQF_Parser p, ODR o, const char *qbuf) +{ + if (!p) + return 0; + p->query_buf = p->query_ptr = qbuf; + p->lex_buf = 0; + return p_query_rpn_mk (o, p, PROTO_Z3950, qbuf); +} + +Z_AttributesPlusTerm *yaz_pqf_scan (YAZ_PQF_Parser p, ODR o, + Odr_oid **attributeSetP, + const char *qbuf) +{ + if (!p) + return 0; + p->query_buf = p->query_ptr = qbuf; + p->lex_buf = 0; + return p_query_scan_mk (p, o, PROTO_Z3950, attributeSetP, qbuf); +} + +int yaz_pqf_error (YAZ_PQF_Parser p, const char **msg, size_t *off) +{ + switch (p->error) + { + case YAZ_PQF_ERROR_NONE: + *msg = "no error"; break; + case YAZ_PQF_ERROR_EXTRA: + *msg = "extra token"; break; + case YAZ_PQF_ERROR_MISSING: + *msg = "missing token"; break; + case YAZ_PQF_ERROR_ATTSET: + *msg = "unknown attribute set"; break; + case YAZ_PQF_ERROR_TOOMANY: + *msg = "too many attributes"; break; + case YAZ_PQF_ERROR_BADATTR: + *msg = "bad attribute specification"; break; + case YAZ_PQF_ERROR_INTERNAL: + *msg = "internal error"; break; + default: + *msg = "unknown error"; break; + } + *off = p->query_ptr - p->query_buf; + return p->error; +}