2 * Copyright (C) 1995-2005, Index Data ApS
5 * $Id: logrpn.c,v 1.11 2005-06-25 15:46:04 adam Exp $
10 * \brief Implements Z39.50 Query Printing
17 #include <yaz/logrpn.h>
20 static const char *relToStr(int v)
25 case 1: str = "Less than"; break;
26 case 2: str = "Less than or equal"; break;
27 case 3: str = "Equal"; break;
28 case 4: str = "Greater or equal"; break;
29 case 5: str = "Greater than"; break;
30 case 6: str = "Not equal"; break;
31 case 100: str = "Phonetic"; break;
32 case 101: str = "Stem"; break;
33 case 102: str = "Relevance"; break;
34 case 103: str = "AlwaysMatches"; break;
39 static void attrStr (int type, int value, enum oid_value ast, char *str)
54 rstr = relToStr(value);
56 sprintf (str, "relation=%s", rstr);
58 sprintf (str, "relation=%d", value);
64 sprintf (str, "position=First in field");
67 sprintf (str, "position=First in any subfield");
70 sprintf (str, "position=Any position in field");
73 sprintf (str, "position");
80 sprintf (str, "structure=Phrase");
83 sprintf (str, "structure=Word");
86 sprintf (str, "structure=Key");
89 sprintf (str, "structure=Year");
92 sprintf (str, "structure=Date");
95 sprintf (str, "structure=Word list");
98 sprintf (str, "structure=Date (un)");
101 sprintf (str, "structure=Name (norm)");
104 sprintf (str, "structure=Name (un)");
107 sprintf (str, "structure=Structure");
110 sprintf (str, "structure=urx");
113 sprintf (str, "structure=free-form-text");
116 sprintf (str, "structure=document-text");
119 sprintf (str, "structure=local-number");
122 sprintf (str, "structure=string");
125 sprintf (str, "structure=numeric string");
128 sprintf (str, "structure");
135 sprintf (str, "truncation=Right");
138 sprintf (str, "truncation=Left");
141 sprintf (str, "truncation=Left&right");
144 sprintf (str, "truncation=Do not truncate");
147 sprintf (str, "truncation=Process #");
150 sprintf (str, "truncation=re-1");
153 sprintf (str, "truncation=re-2");
156 sprintf (str, "truncation=CCL");
159 sprintf (str, "truncation");
166 sprintf (str, "completeness=Incomplete subfield");
169 sprintf (str, "completeness=Complete subfield");
172 sprintf (str, "completeness=Complete field");
175 sprintf (str, "completeness");
184 sprintf (str + strlen(str), " (%d=%d)", type, value);
186 sprintf (str, "%d=%d", type, value);
189 static void wrbuf_term(WRBUF b, const char *term, int len)
192 for (i = 0; i < len; i++)
193 if (strchr(" \"{", term[i]))
196 wrbuf_printf(b, "%.*s ", len, term);
200 for (i = 0; i<len; i++)
204 wrbuf_putc(b, term[i]);
206 wrbuf_printf(b, "\" ");
210 static void wrbuf_attr(WRBUF b, Z_AttributeElement *element)
214 char *sep = ""; /* optional space after attrset name */
215 if (element->attributeSet)
218 attrset = oid_getentbyoid (element->attributeSet);
219 setname = attrset->desc;
222 switch (element->which)
224 case Z_AttributeValue_numeric:
225 wrbuf_printf(b,"@attr %s%s%d=%d ", setname, sep,
226 *element->attributeType, *element->value.numeric);
228 case Z_AttributeValue_complex:
229 wrbuf_printf(b,"@attr %s%s\"%d=", setname, sep,
230 *element->attributeType);
231 for (i = 0; i<element->value.complex->num_list; i++)
235 if (element->value.complex->list[i]->which ==
236 Z_StringOrNumeric_string)
237 wrbuf_printf (b, "%s",
238 element->value.complex->list[i]->u.string);
239 else if (element->value.complex->list[i]->which ==
240 Z_StringOrNumeric_numeric)
241 wrbuf_printf (b, "%d",
242 *element->value.complex->list[i]->u.numeric);
244 wrbuf_printf(b, "\" ");
247 wrbuf_printf (b, "@attr 1=unknown ");
252 * zlog_attributes: print attributes of term
254 static void zlog_attributes (Z_AttributesPlusTerm *t, int depth,
255 enum oid_value ast, int loglevel)
259 int num_attributes = t->attributes->num_attributes;
261 for (of = 0; of < num_attributes; of++)
263 const char *attset_name = "";
264 Z_AttributeElement *element;
265 element = t->attributes->attributes[of];
266 if (element->attributeSet)
269 attrset = oid_getentbyoid (element->attributeSet);
270 attset_name = attrset->desc;
272 switch (element->which)
274 case Z_AttributeValue_numeric:
275 attrStr (*element->attributeType,
276 *element->value.numeric, ast, str);
277 yaz_log (loglevel, "%*.0s%s %s", depth, "", attset_name, str);
279 case Z_AttributeValue_complex:
280 yaz_log (loglevel, "%*.0s%s attributeType=%d complex",
281 depth, "", attset_name, *element->attributeType);
282 for (i = 0; i<element->value.complex->num_list; i++)
284 if (element->value.complex->list[i]->which ==
285 Z_StringOrNumeric_string)
286 yaz_log (loglevel, "%*.0s string: '%s'", depth, "",
287 element->value.complex->list[i]->u.string);
288 else if (element->value.complex->list[i]->which ==
289 Z_StringOrNumeric_numeric)
290 yaz_log (loglevel, "%*.0s numeric: '%d'", depth, "",
291 *element->value.complex->list[i]->u.numeric);
295 yaz_log (loglevel, "%.*s%s attribute unknown",
296 depth, "", attset_name);
301 static char *complex_op_name(Z_Operator *op)
309 case Z_Operator_and_not:
311 case Z_Operator_prox:
314 return "unknown complex operator";
318 static char *prox_unit_name(Z_ProximityOperator *op)
320 if (op->which!=Z_ProximityOperator_known)
324 case Z_ProxUnit_character: return "character";
325 case Z_ProxUnit_word: return "word";
326 case Z_ProxUnit_sentence: return "sentence";
327 case Z_ProxUnit_paragraph: return "paragraph";
328 case Z_ProxUnit_section: return "section";
329 case Z_ProxUnit_chapter: return "chapter";
330 case Z_ProxUnit_document: return "document";
331 case Z_ProxUnit_element: return "element";
332 case Z_ProxUnit_subelement: return "subelement";
333 case Z_ProxUnit_elementType: return "elementType";
334 case Z_ProxUnit_byte: return "byte";
335 default: return "unknown";
339 static void zlog_structure (Z_RPNStructure *zs, int depth,
340 enum oid_value ast, int loglevel)
342 if (zs->which == Z_RPNStructure_complex)
344 Z_Operator *op = zs->u.complex->roperator;
349 case Z_Operator_and_not:
350 yaz_log (loglevel, "%*.0s %s", depth, "", complex_op_name(op) );
352 case Z_Operator_prox:
353 yaz_log (loglevel, "%*.0s prox excl=%s dist=%d order=%s "
355 depth, "", op->u.prox->exclusion ?
356 (*op->u.prox->exclusion ? "T" : "F") : "N",
357 *op->u.prox->distance,
358 *op->u.prox->ordered ? "T" : "F",
359 relToStr(*op->u.prox->relationType),
360 prox_unit_name(op->u.prox) );
363 yaz_log (loglevel, "%*.0s unknown complex", depth, "");
366 zlog_structure (zs->u.complex->s1, depth+2, ast, loglevel);
367 zlog_structure (zs->u.complex->s2, depth+2, ast, loglevel);
369 else if (zs->which == Z_RPNStructure_simple)
371 if (zs->u.simple->which == Z_Operand_APT)
373 Z_AttributesPlusTerm *zapt = zs->u.simple->u.attributesPlusTerm;
375 switch (zapt->term->which)
378 yaz_log (loglevel, "%*.0s term '%.*s' (general)", depth, "",
379 zapt->term->u.general->len,
380 zapt->term->u.general->buf);
382 case Z_Term_characterString:
383 yaz_log (loglevel, "%*.0s term '%s' (string)", depth, "",
384 zapt->term->u.characterString);
387 yaz_log (loglevel, "%*.0s term '%d' (numeric)", depth, "",
388 *zapt->term->u.numeric);
391 yaz_log (loglevel, "%*.0s term (null)", depth, "");
394 yaz_log (loglevel, "%*.0s term (not general)", depth, "");
396 zlog_attributes (zapt, depth+2, ast, loglevel);
398 else if (zs->u.simple->which == Z_Operand_resultSetId)
400 yaz_log (loglevel, "%*.0s set '%s'", depth, "",
401 zs->u.simple->u.resultSetId);
404 yaz_log (loglevel, "%*.0s unknown simple structure", depth, "");
407 yaz_log (loglevel, "%*.0s unknown structure", depth, "");
410 static void wrbuf_apt(WRBUF b, Z_AttributesPlusTerm *zapt)
412 int num_attributes = zapt->attributes->num_attributes;
414 for (i = 0; i<num_attributes; i++)
415 wrbuf_attr(b,zapt->attributes->attributes[i]);
417 switch (zapt->term->which)
420 wrbuf_term(b, zapt->term->u.general->buf,
421 zapt->term->u.general->len);
423 case Z_Term_characterString:
424 wrbuf_printf(b, "@term string ");
425 wrbuf_term (b, zapt->term->u.characterString,
426 strlen(zapt->term->u.characterString));
429 wrbuf_printf(b, "@term numeric %d ", *zapt->term->u.numeric);
432 wrbuf_printf(b, "@term null x");
435 wrbuf_printf(b, "@term null unknown%d ", zapt->term->which);
439 static void wrbuf_structure (WRBUF b, Z_RPNStructure *zs, enum oid_value ast)
441 if (zs->which == Z_RPNStructure_complex)
443 Z_Operator *op = zs->u.complex->roperator;
444 wrbuf_printf(b, "@%s ", complex_op_name(op) );
445 if (op->which== Z_Operator_prox)
447 if (!op->u.prox->exclusion)
449 else if (*op->u.prox->exclusion)
454 wrbuf_printf(b, " %d %d %d ", *op->u.prox->distance,
455 *op->u.prox->ordered,
456 *op->u.prox->relationType);
458 switch(op->u.prox->which)
460 case Z_ProximityOperator_known:
463 case Z_ProximityOperator_private:
467 wrbuf_printf(b, "%d", op->u.prox->which);
469 if (op->u.prox->u.known)
470 wrbuf_printf(b, " %d ", *op->u.prox->u.known);
472 wrbuf_printf(b, " 0 ");
474 wrbuf_structure (b,zs->u.complex->s1, ast);
475 wrbuf_structure (b,zs->u.complex->s2, ast);
477 else if (zs->which == Z_RPNStructure_simple)
479 if (zs->u.simple->which == Z_Operand_APT)
480 wrbuf_apt(b, zs->u.simple->u.attributesPlusTerm);
481 else if (zs->u.simple->which == Z_Operand_resultSetId)
483 wrbuf_printf(b, "@set ");
484 wrbuf_term(b, zs->u.simple->u.resultSetId,
485 strlen(zs->u.simple->u.resultSetId));
488 wrbuf_printf (b, "(unknown simple structure)");
491 wrbuf_puts(b, "(unknown structure)");
494 void log_rpn_query_level (int loglevel, Z_RPNQuery *rpn)
499 attrset = oid_getentbyoid (rpn->attributeSetId);
502 ast = attrset->value;
503 yaz_log (loglevel, "RPN query. Type: %s", attrset->desc);
508 yaz_log (loglevel, "RPN query. Unknown type");
510 zlog_structure (rpn->RPNStructure, 0, ast, loglevel);
513 static void wrbuf_rpn_query(WRBUF b, Z_RPNQuery *rpn)
518 attrset = oid_getentbyoid (rpn->attributeSetId);
521 ast = attrset->value;
522 wrbuf_printf(b, " @attrset %s ", attrset->desc);
527 wrbuf_printf (b, "Unknown:");
529 wrbuf_structure (b,rpn->RPNStructure, ast);
533 void log_rpn_query (Z_RPNQuery *rpn)
535 log_rpn_query_level(YLOG_LOG, rpn);
538 void log_scan_term_level (int loglevel,
539 Z_AttributesPlusTerm *zapt, oid_value ast)
544 if (zapt->term->which == Z_Term_general)
546 yaz_log (loglevel, "%*.0s term '%.*s' (general)", depth, "",
547 zapt->term->u.general->len, zapt->term->u.general->buf);
550 yaz_log (loglevel, "%*.0s term (not general)", depth, "");
551 zlog_attributes (zapt, depth+2, ast, loglevel);
554 void log_scan_term (Z_AttributesPlusTerm *zapt, oid_value ast)
556 log_scan_term_level (YLOG_LOG, zapt, ast);
559 void wrbuf_scan_term(WRBUF b, Z_AttributesPlusTerm *zapt, oid_value ast)
564 void yaz_log_zquery_level (int loglevel, Z_Query *q)
570 case Z_Query_type_1: case Z_Query_type_101:
571 log_rpn_query_level (loglevel, q->u.type_1);
574 yaz_log(loglevel, "CCL: %.*s", q->u.type_2->len, q->u.type_2->buf);
576 case Z_Query_type_100:
577 yaz_log(loglevel, "Z39.58: %.*s", q->u.type_100->len,
580 case Z_Query_type_104:
581 if (q->u.type_104->which == Z_External_CQL)
582 yaz_log (loglevel, "CQL: %s", q->u.type_104->u.cql);
586 void yaz_log_zquery (Z_Query *q)
588 yaz_log_zquery_level(YLOG_LOG,q);
591 void wrbuf_put_zquery(WRBUF b, Z_Query *q)
598 case Z_Query_type_101:
599 wrbuf_printf(b,"RPN:");
600 wrbuf_rpn_query(b,q->u.type_1);
603 wrbuf_printf(b, "CCL: %.*s", q->u.type_2->len, q->u.type_2->buf);
605 case Z_Query_type_100:
606 wrbuf_printf(b, "Z39.58: %.*s", q->u.type_100->len,
609 case Z_Query_type_104:
610 if (q->u.type_104->which == Z_External_CQL)
611 wrbuf_printf(b, "CQL: %s", q->u.type_104->u.cql);
613 wrbuf_printf(b,"Unknown type 104 query %d", q->u.type_104->which);
617 void wrbuf_diags(WRBUF b, int num_diagnostics,Z_DiagRec **diags)
619 /* we only dump the first diag - that keeps the log cleaner. */
620 wrbuf_printf(b," ERROR ");
621 if (diags[0]->which != Z_DiagRec_defaultFormat)
622 wrbuf_printf(b,"(diag not in default format?)");
625 Z_DefaultDiagFormat *e=diags[0]->u.defaultFormat;
627 wrbuf_printf(b, "%d ",*e->condition);
629 wrbuf_printf(b, "?? ");
630 if ((e->which==Z_DefaultDiagFormat_v2Addinfo) && (e->u.v2Addinfo))
631 wrbuf_printf(b,"%s ",e->u.v2Addinfo);
632 else if ((e->which==Z_DefaultDiagFormat_v3Addinfo) && (e->u.v3Addinfo))
633 wrbuf_printf(b,"%s ",e->u.v3Addinfo);
639 * indent-tabs-mode: nil
641 * vim: shiftwidth=4 tabstop=8 expandtab