Improved installation. Moved header files to include/yaz.
[yaz-moved-to-github.git] / zutil / logrpn.c
1 /*
2  * Copyright (C) 1995-1999, Index Data
3  * All rights reserved.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: logrpn.c,v $
7  * Revision 1.3  1999-11-30 13:47:12  adam
8  * Improved installation. Moved header files to include/yaz.
9  *
10  * Revision 1.2  1999/08/27 09:40:33  adam
11  * Renamed logf function to yaz_log. Removed VC++ project files.
12  *
13  * Revision 1.1  1999/06/08 10:10:16  adam
14  * New sub directory zutil. Moved YAZ Compiler to be part of YAZ tree.
15  *
16  * Revision 1.1  1998/11/16 16:02:35  adam
17  * Added loggin utilies, log_rpn_query and log_scan_term. These used
18  * to be part of Zebra.
19  *
20  *
21  *
22  * Old logs from zlogs.c:
23  *
24  * Revision 1.9  1998/10/13 20:36:02  adam
25  * Changed "indent" format string in log messages.
26  *
27  * Revision 1.8  1998/04/02 14:35:29  adam
28  * First version of Zebra that works with compiled ASN.1.
29  *
30  * Revision 1.7  1998/01/29 13:40:11  adam
31  * Better logging for scan service.
32  *
33  * Revision 1.6  1997/09/29 09:06:41  adam
34  * Removed static var to make this module thread safe.
35  *
36  * Revision 1.5  1997/04/30 08:56:07  quinn
37  * null
38  *
39  * Revision 1.4  1996/10/08  09:41:25  quinn
40  * Fixed diagnostic.
41  *
42  * Revision 1.3  1996/03/20  09:36:40  adam
43  * Function dict_lookup_grep got extra parameter, init_pos, which marks
44  * from which position in pattern approximate pattern matching should occur.
45  * Approximate pattern matching is used in relevance=re-2.
46  *
47  * Revision 1.2  1996/01/03  16:22:11  quinn
48  * operator->roperator
49  *
50  * Revision 1.1  1995/11/16  17:00:55  adam
51  * Better logging of rpn query.
52  *
53  */
54 #include <stdio.h>
55 #include <assert.h>
56
57 #include <yaz/log.h>
58 #include <yaz/logrpn.h>
59
60 static void attrStr (int type, int value, enum oid_value ast, char *str)
61 {
62     *str = '\0';
63     switch (ast)
64     {
65     case VAL_BIB1:
66     case VAL_EXP1:
67     case VAL_GILS:
68         switch (type)
69         {
70         case 1:
71             sprintf (str, "use");
72             break;
73         case 2:
74             switch (value)
75             {
76             case 1:
77                 sprintf (str, "relation=Less than");
78                 break;
79             case 2:
80                 sprintf (str, "relation=Less than or equal");
81                 break;
82             case 3:
83                 sprintf (str, "relation=Equal");
84                 break;
85             case 4:
86                 sprintf (str, "relation=Greater or equal");
87                 break;
88             case 5:
89                 sprintf (str, "relation=Greater than");
90                 break;
91             case 6:
92                 sprintf (str, "relation=Not equal");
93                 break;
94             case 100:
95                 sprintf (str, "relation=Phonetic");
96                 break;
97             case 101:
98                 sprintf (str, "relation=Stem");
99                 break;
100             case 102:
101                 sprintf (str, "relation=Relevance");
102                 break;
103             case 103:
104                 sprintf (str, "relation=AlwaysMatches");
105                 break;
106             default:
107                 sprintf (str, "relation");
108             }
109             break;
110         case 3:
111             switch (value)
112             {
113             case 1:
114                 sprintf (str, "position=First in field");
115                 break;
116             case 2:
117                 sprintf (str, "position=First in any subfield");
118                 break;
119             case 3:
120                 sprintf (str, "position=Any position in field");
121                 break;
122             default:
123                 sprintf (str, "position");
124             }
125             break;
126         case 4:
127             switch (value)
128             {
129             case 1:
130                 sprintf (str, "structure=Phrase");
131                 break;
132             case 2:
133                 sprintf (str, "structure=Word");
134                 break;
135             case 3:
136                 sprintf (str, "structure=Key");
137                 break;
138             case 4:
139                 sprintf (str, "structure=Year");
140                 break;
141             case 5:
142                 sprintf (str, "structure=Date");
143                 break;
144             case 6:
145                 sprintf (str, "structure=Word list");
146                 break;
147             case 100:
148                 sprintf (str, "structure=Date (un)");
149                 break;
150             case 101:
151                 sprintf (str, "structure=Name (norm)");
152                 break;
153             case 102:
154                 sprintf (str, "structure=Name (un)");
155                 break;
156             case 103:
157                 sprintf (str, "structure=Structure");
158                 break;
159             case 104:
160                 sprintf (str, "structure=urx");
161                 break;
162             case 105:
163                 sprintf (str, "structure=free-form-text");
164                 break;
165             case 106:
166                 sprintf (str, "structure=document-text");
167                 break;
168             case 107:
169                 sprintf (str, "structure=local-number");
170                 break;
171             case 108:
172                 sprintf (str, "structure=string");
173                 break;
174             case 109:
175                 sprintf (str, "structure=numeric string");
176                 break;
177             default:
178                 sprintf (str, "structure");
179             }
180             break;
181         case 5:
182             switch (value)
183             {
184             case 1:
185                 sprintf (str, "truncation=Right");
186                 break;
187             case 2:
188                 sprintf (str, "truncation=Left");
189                 break;
190             case 3:
191                 sprintf (str, "truncation=Left&right");
192                 break;
193             case 100:
194                 sprintf (str, "truncation=Do not truncate");
195                 break;
196             case 101:
197                 sprintf (str, "truncation=Process #");
198                 break;
199             case 102:
200                 sprintf (str, "truncation=re-1");
201                 break;
202             case 103:
203                 sprintf (str, "truncation=re-2");
204                 break;
205             default:
206                 sprintf (str, "truncation");
207             }
208             break;
209         case 6:
210             switch (value)
211             {
212             case 1:
213                 sprintf (str, "completeness=Incomplete subfield");
214                 break;
215             case 2:
216                 sprintf (str, "completeness=Complete subfield");
217                 break;
218             case 3:
219                 sprintf (str, "completeness=Complete field");
220                 break;
221             default:
222                 sprintf (str, "completeness");
223             }
224             break;
225         }
226         break;
227     default:
228         break;
229     }
230     if (*str)
231         sprintf (str + strlen(str), " (%d=%d)", type, value);
232     else
233         sprintf (str, "%d=%d", type, value);
234 }
235
236 /*
237  * zlog_attributes: print attributes of term
238  */
239 static void zlog_attributes (Z_AttributesPlusTerm *t, int level,
240                              enum oid_value ast)
241 {
242     int of, i;
243     char str[80];
244 #ifdef ASN_COMPILED
245     int num_attributes = t->attributes->num_attributes;
246 #else
247     int num_attributes = t->num_attributes;
248 #endif
249     
250     for (of = 0; of < num_attributes; of++)
251     {
252         const char *attset_name = "";
253         Z_AttributeElement *element;
254 #ifdef ASN_COMPILED
255         element = t->attributes->attributes[of];
256 #else
257         element = t->attributeList[of];
258 #endif
259         if (element->attributeSet)
260         {
261             oident *attrset;
262             attrset = oid_getentbyoid (element->attributeSet);
263             attset_name = attrset->desc;
264         }
265         switch (element->which) 
266         {
267         case Z_AttributeValue_numeric:
268             attrStr (*element->attributeType,
269                      *element->value.numeric, ast, str);
270             yaz_log (LOG_LOG, "%*.0s%s %s", level, "", attset_name, str);
271             break;
272         case Z_AttributeValue_complex:
273             yaz_log (LOG_LOG, "%*.0s%s attributeType=%d complex",
274                   level, "", attset_name, *element->attributeType);
275             for (i = 0; i<element->value.complex->num_list; i++)
276             {
277                 if (element->value.complex->list[i]->which ==
278                     Z_StringOrNumeric_string)
279                     yaz_log (LOG_LOG, "%*.0s  string: '%s'", level, "",
280                              element->value.complex->list[i]->u.string);
281                 else if (element->value.complex->list[i]->which ==
282                          Z_StringOrNumeric_numeric)
283                     yaz_log (LOG_LOG, "%*.0s  numeric: '%d'", level, "",
284                              *element->value.complex->list[i]->u.numeric);
285             }
286             break;
287         default:
288             yaz_log (LOG_LOG, "%.*s%s attribute unknown",
289                      level, "", attset_name);
290         }
291     }
292 }
293
294 static void zlog_structure (Z_RPNStructure *zs, int level, enum oid_value ast)
295 {
296     if (zs->which == Z_RPNStructure_complex)
297     {
298         switch (zs->u.complex->roperator->which)
299         {
300         case Z_Operator_and:
301             yaz_log (LOG_LOG, "%*.0s and", level, "");
302             break;
303         case Z_Operator_or:
304             yaz_log (LOG_LOG, "%*.0s or", level, "");
305             break;
306         case Z_Operator_and_not:
307             yaz_log (LOG_LOG, "%*.0s and-not", level, "");
308             break;
309         default:
310             yaz_log (LOG_LOG, "%*.0s unknown complex", level, "");
311             return;
312         }
313         zlog_structure (zs->u.complex->s1, level+2, ast);
314         zlog_structure (zs->u.complex->s2, level+2, ast);
315     }
316     else if (zs->which == Z_RPNStructure_simple)
317     {
318         if (zs->u.simple->which == Z_Operand_APT)
319         {
320             Z_AttributesPlusTerm *zapt = zs->u.simple->u.attributesPlusTerm;
321
322             if (zapt->term->which == Z_Term_general) 
323             {
324                 yaz_log (LOG_LOG, "%*.0s term '%.*s' (general)", level, "",
325                          zapt->term->u.general->len,
326                          zapt->term->u.general->buf);
327             }
328             else
329             {
330                 yaz_log (LOG_LOG, "%*.0s term (not general)", level, "");
331             }
332             zlog_attributes (zapt, level+2, ast);
333         }
334         else if (zs->u.simple->which == Z_Operand_resultSetId)
335         {
336             yaz_log (LOG_LOG, "%*.0s set '%s'", level, "",
337                      zs->u.simple->u.resultSetId);
338         }
339         else
340             yaz_log (LOG_LOG, "%*.0s unknown simple structure", level, "");
341     }
342     else
343         yaz_log (LOG_LOG, "%*.0s unknown structure", level, "");
344 }
345
346 void log_rpn_query (Z_RPNQuery *rpn)
347 {
348     oident *attrset;
349     enum oid_value ast;
350     
351     attrset = oid_getentbyoid (rpn->attributeSetId);
352     if (attrset)
353     {
354         ast = attrset->value;
355         yaz_log (LOG_LOG, "RPN query. Type: %s", attrset->desc);
356     } 
357     else
358     {
359         ast = VAL_NONE;
360         yaz_log (LOG_LOG, "RPN query. Unknown type");
361     }
362     zlog_structure (rpn->RPNStructure, 0, ast);
363 }
364
365 void log_scan_term (Z_AttributesPlusTerm *zapt, oid_value ast)
366 {
367     int level = 0;
368     if (zapt->term->which == Z_Term_general) 
369     {
370         yaz_log (LOG_LOG, "%*.0s term '%.*s' (general)", level, "",
371                  zapt->term->u.general->len, zapt->term->u.general->buf);
372     }
373     else
374         yaz_log (LOG_LOG, "%*.0s term (not general)", level, "");
375     zlog_attributes (zapt, level+2, ast);
376 }