/*
- * Copyright (c) 1995-1998, Index Data.
+ * Copyright (c) 1995-2001, Index Data.
* See the file LICENSE for details.
- * Sebastian Hammer, Adam Dickmeiss
*
* $Log: pquery.c,v $
- * Revision 1.6 2001-03-07 13:24:40 adam
+ * Revision 1.7 2001-05-09 23:31:35 adam
+ * String attribute values for PQF. Proper C-backslash escaping for PQF.
+ *
+ * Revision 1.6 2001/03/07 13:24:40 adam
* Member and_not in Z_Operator is kept for backwards compatibility.
* Added support for definition of CCL operators in field spec file.
*
static Z_RPNStructure *rpn_structure (struct lex_info *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 int query_token (struct lex_info *li)
{
+ int sep_char = ' ';
const char *sep_match;
const char **qptr = &li->query_buf;
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;
+
+ while (**qptr && **qptr != sep_char)
{
- li->lex_buf = *qptr;
- while (**qptr && **qptr != ' ')
- {
- ++(li->lex_len);
- ++(*qptr);
- }
+ if (**qptr == '\\')
+ {
+ ++(li->lex_len);
+ ++(*qptr);
+ }
+ ++(li->lex_len);
+ ++(*qptr);
}
+ if (**qptr)
+ ++(*qptr);
if (li->lex_len >= 1 && li->lex_buf[0] == li->escape_char)
{
if (compare_term (li, "and", 1))
return li->query_look = query_token (li);
}
+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 lex_info *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)
+ return 0;
+ lex (li);
+
+ if (!(cp = strchr (li->lex_buf, '=')))
+ 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] = 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 lex_info *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;
elements[k]->attributeSet =
odr_oiddup (o, oid_ent_to_oid (&attrid, oid));
}
- elements[k]->which = Z_AttributeValue_numeric;
- elements[k]->value.numeric = &attr_tmp[2*i+1];
+ 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;
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->size = term_octet->len =
+ escape_string (term_octet->buf, li->lex_buf, li->lex_len);
return zapt;
}
static Z_Operand *rpn_simple (struct lex_info *li, ODR o, oid_proto proto,
- int num_attr, int *attr_list,
+ int num_attr, int *attr_list, char **attr_clist,
oid_value *attr_set)
{
Z_Operand *zo;
case 't':
zo->which = Z_Operand_APT;
if (!(zo->u.attributesPlusTerm =
- rpn_term (li, o, proto, num_attr, attr_list, attr_set)))
+ rpn_term (li, o, proto, num_attr, attr_list, attr_clist,
+ attr_set)))
return NULL;
lex (li);
break;
static Z_Complex *rpn_complex (struct lex_info *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;
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,
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;
sz = (Z_RPNStructure *)odr_malloc (o, sizeof(*sz));
switch (li->query_look)
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':
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':
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;
- }
- 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);
- attr_list[2*num_attr+1] = atoi (cp+1);
+ p_query_parse_attr(li, o, num_attr, attr_list, attr_clist, attr_set);
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)
lex (li);
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;
}
{
Z_RPNQuery *zq;
int attr_array[1024];
+ char *attr_clist[512];
oid_value attr_set[512];
oid_value topSet = VAL_NONE;
oident oset;
zq->attributeSetId = odr_oiddup (o, oid);
if (!(zq->RPNStructure = rpn_structure (li, o, proto, 0, 512,
- attr_array, attr_set)))
+ attr_array, attr_clist, attr_set)))
return NULL;
return zq;
}
return p_query_rpn_mk (o, &li, proto, qbuf);
}
+
Z_AttributesPlusTerm *p_query_scan_mk (struct lex_info *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];
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);
- lex (li);
-
- if (!(cp = strchr (li->lex_buf, '=')))
- return NULL;
- }
- 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);
- attr_list[2*num_attr+1] = atoi (cp+1);
+ p_query_parse_attr(li, o, num_attr, attr_list, attr_clist, attr_set);
num_attr++;
lex (li);
}
if (!li->query_look)
return NULL;
- return rpn_term (li, o, proto, num_attr, attr_list, attr_set);
+ return rpn_term (li, o, proto, num_attr, attr_list, attr_clist, attr_set);
}
Z_AttributesPlusTerm *p_query_scan (ODR o, oid_proto proto,