Doxygen frindly comments
[yaz-moved-to-github.git] / ztest / ztest.c
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) 1995-2009 Index Data
3  * See the file LICENSE for details.
4  */
5
6 /** \file
7  * \brief yaz-ztest Generic Frontend Server
8  */
9
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <ctype.h>
13 #if HAVE_UNISTD_H
14 #include <unistd.h>
15 #endif
16
17 #include <yaz/log.h>
18 #include <yaz/backend.h>
19 #include <yaz/ill.h>
20 #include <yaz/diagbib1.h>
21
22 #include "ztest.h"
23
24 static int log_level=0;
25 static int log_level_set=0;
26
27 int ztest_search(void *handle, bend_search_rr *rr);
28 int ztest_sort(void *handle, bend_sort_rr *rr);
29 int ztest_present(void *handle, bend_present_rr *rr);
30 int ztest_esrequest(void *handle, bend_esrequest_rr *rr);
31 int ztest_delete(void *handle, bend_delete_rr *rr);
32
33 /** \brief use term value as hit count 
34     \param s RPN structure
35     \return >= 0: search term number or -1: not found
36    
37     Traverse RPN tree 'in order' and use term value as hit count.
38     Only terms  that looks a numeric is used.. Returns -1 if
39     no sub tree has a hit count term
40 */
41 static Odr_int get_term_hit(Z_RPNStructure *s)
42 {
43     Odr_int h = -1;
44     switch(s->which)
45     {
46     case Z_RPNStructure_simple:
47         if (s->u.simple->which == Z_Operand_APT)
48         {
49             Z_AttributesPlusTerm *apt = s->u.simple->u.attributesPlusTerm;
50             if (apt->term->which == Z_Term_general)
51             {
52                 Odr_oct *oct = apt->term->u.general;
53                 if (oct->len > 0 && oct->buf[0] >= '0' && oct->buf[0] <= '9')
54                 {
55                     char *endptr;
56                     WRBUF hits_str = wrbuf_alloc();
57                     wrbuf_write(hits_str, (const char *) oct->buf, oct->len);
58                     h = odr_atoi(wrbuf_cstr(hits_str));
59                     wrbuf_destroy(hits_str);
60                 }
61             }
62         }
63         break;
64     case Z_RPNStructure_complex:
65         h = get_term_hit(s->u.complex->s1);
66         if (h == -1)
67             h = get_term_hit(s->u.complex->s2);
68         break;
69     }
70     return h;
71 }
72
73 /** \brief gets hit count for numeric terms in RPN queries
74     \param q RPN Query
75     \return number of hits (random or number for term)
76     
77     This is just for testing.. A real database of course uses
78     the content of a database to establish a value.. In our case, we
79     have a way to trigger a certain hit count. Good for testing of
80     client applications etc
81 */
82 static Odr_int get_hit_count(Z_Query *q)
83 {
84     Odr_int h = -1;
85     if (q->which == Z_Query_type_1 || q->which == Z_Query_type_101)
86         h = get_term_hit(q->u.type_1->RPNStructure);
87     if (h == -1)
88         h = rand() % 24;
89     return h;
90 }
91
92 /** \brief checks if it's a dummy Slow database
93     \param basename database name to check
94     \param association backend association (or NULL if not available)
95     \retval 1 is slow database
96     \retval 0 is not a slow database
97
98     The Slow database is for testing.. It allows us to simulate
99     a slow server...
100 */
101 static int check_slow(const char *basename, bend_association association)
102 {
103     if (strncmp(basename, "Slow", 4) == 0)
104     {
105 #if HAVE_UNISTD_H
106         int i, w = 3;
107         if (basename[4])
108             sscanf(basename+4, "%d", &w);
109         /* wait up to 3 seconds and check if connection is still alive */
110         for (i = 0; i < w; i++)
111         {
112             if (association && !bend_assoc_is_alive(association))
113             {
114                 yaz_log(YLOG_LOG, "search aborted");
115                 break;
116             }
117             sleep(1);
118         }
119 #endif
120         return 1;
121     }
122     return 0;
123 }
124
125 int ztest_search(void *handle, bend_search_rr *rr)
126 {
127     if (rr->num_bases != 1)
128     {
129         rr->errcode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
130         return 0;
131     }
132     /* Throw Database unavailable if other than Default or Slow */
133     if (!yaz_matchstr(rr->basenames[0], "Default"))
134         ;  /* Default is OK in our test */
135     else if (check_slow(rr->basenames[0], rr->association))
136     {
137         rr->estimated_hit_count = 1;
138     }
139     else
140     {
141         rr->errcode = YAZ_BIB1_DATABASE_UNAVAILABLE;
142         rr->errstring = rr->basenames[0];
143         return 0;
144     }
145
146     if (rr->extra_args)
147     {
148         Z_SRW_extra_arg *a;
149         WRBUF response_xml = wrbuf_alloc();
150         wrbuf_puts(response_xml, "<extra>");
151         for (a = rr->extra_args; a; a = a->next)
152         {
153             wrbuf_puts(response_xml, "<extra name=\"");
154             wrbuf_xmlputs(response_xml, a->name);
155             wrbuf_puts(response_xml, "\"");
156             if (a->value)
157             {
158                 wrbuf_puts(response_xml, " value=\"");
159                 wrbuf_xmlputs(response_xml, a->value);
160                 wrbuf_puts(response_xml, "\"");
161             }
162             wrbuf_puts(response_xml, "/>");
163         }
164         wrbuf_puts(response_xml, "</extra>");
165         rr->extra_response_data =
166             odr_strdup(rr->stream, wrbuf_cstr(response_xml));
167         wrbuf_destroy(response_xml);
168     }
169     rr->hits = get_hit_count(rr->query);
170     return 0;
171 }
172
173
174 /* this huge function handles extended services */
175 int ztest_esrequest(void *handle, bend_esrequest_rr *rr)
176 {
177     /* user-defined handle - created in bend_init */
178     int *counter = (int*) handle;  
179
180     yaz_log(log_level, "ESRequest no %d", *counter);
181
182     (*counter)++;
183
184     if (rr->esr->packageName)
185         yaz_log(log_level, "packagename: %s", rr->esr->packageName);
186     yaz_log(log_level, "Waitaction: " ODR_INT_PRINTF, *rr->esr->waitAction);
187
188
189     yaz_log(log_level, "function: " ODR_INT_PRINTF, *rr->esr->function);
190
191     if (!rr->esr->taskSpecificParameters)
192     {
193         yaz_log(log_level, "No task specific parameters");
194     }
195     else if (rr->esr->taskSpecificParameters->which == Z_External_itemOrder)
196     {
197         Z_ItemOrder *it = rr->esr->taskSpecificParameters->u.itemOrder;
198         yaz_log(log_level, "Received ItemOrder");
199         if (it->which == Z_IOItemOrder_esRequest)
200         {
201             Z_IORequest *ir = it->u.esRequest;
202             Z_IOOriginPartToKeep *k = ir->toKeep;
203             Z_IOOriginPartNotToKeep *n = ir->notToKeep;
204             const char *xml_in_response = 0;
205             
206             if (k && k->contact)
207             {
208                 if (k->contact->name)
209                     yaz_log(log_level, "contact name %s", k->contact->name);
210                 if (k->contact->phone)
211                     yaz_log(log_level, "contact phone %s", k->contact->phone);
212                 if (k->contact->email)
213                     yaz_log(log_level, "contact email %s", k->contact->email);
214             }
215             if (k->addlBilling)
216             {
217                 yaz_log(log_level, "Billing info (not shown)");
218             }
219             
220             if (n->resultSetItem)
221             {
222                 yaz_log(log_level, "resultsetItem");
223                 yaz_log(log_level, "setId: %s", n->resultSetItem->resultSetId);
224                 yaz_log(log_level, "item: " ODR_INT_PRINTF, *n->resultSetItem->item);
225             }
226             if (n->itemRequest)
227             {
228                 Z_External *r = (Z_External*) n->itemRequest;
229                 ILL_ItemRequest *item_req = 0;
230                 ILL_APDU *ill_apdu = 0;
231                 if (r->direct_reference)
232                 {
233                     char oid_name_str[OID_STR_MAX];
234                     oid_class oclass;
235                     const char *oid_name = 
236                         yaz_oid_to_string_buf(r->direct_reference,
237                                               &oclass, oid_name_str);
238                     if (oid_name)
239                         yaz_log(log_level, "OID %s", oid_name);
240                     if (!oid_oidcmp(r->direct_reference, yaz_oid_recsyn_xml))
241                     {
242                         yaz_log(log_level, "ILL XML request");
243                         if (r->which == Z_External_octet)
244                             yaz_log(log_level, "%.*s",
245                                     r->u.octet_aligned->len,
246                                     r->u.octet_aligned->buf); 
247                         xml_in_response = "<dummy>x</dummy>";
248                     }
249                     if (!oid_oidcmp(r->direct_reference, 
250                                     yaz_oid_general_isoill_1))
251                     {
252                         yaz_log(log_level, "Decode ItemRequest begin");
253                         if (r->which == ODR_EXTERNAL_single)
254                         {
255                             odr_setbuf(rr->decode,
256                                        (char *) r->u.single_ASN1_type->buf,
257                                        r->u.single_ASN1_type->len, 0);
258                             
259                             if (!ill_ItemRequest(rr->decode, &item_req, 0, 0))
260                             {
261                                 yaz_log(log_level,
262                                         "Couldn't decode ItemRequest %s near %ld",
263                                         odr_errmsg(odr_geterror(rr->decode)),
264                                         (long) odr_offset(rr->decode));
265                             }
266                             else
267                                 yaz_log(log_level, "Decode ItemRequest OK");
268                             if (rr->print)
269                             {
270                                 ill_ItemRequest(rr->print, &item_req, 0,
271                                                 "ItemRequest");
272                                 odr_reset(rr->print);
273                             }
274                         }
275                         if (!item_req && r->which == ODR_EXTERNAL_single)
276                         {
277                             yaz_log(log_level, "Decode ILL APDU begin");
278                             odr_setbuf(rr->decode,
279                                        (char*) r->u.single_ASN1_type->buf,
280                                        r->u.single_ASN1_type->len, 0);
281                             
282                             if (!ill_APDU(rr->decode, &ill_apdu, 0, 0))
283                             {
284                                 yaz_log(log_level,
285                                         "Couldn't decode ILL APDU %s near %ld",
286                                         odr_errmsg(odr_geterror(rr->decode)),
287                                         (long) odr_offset(rr->decode));
288                                 yaz_log(log_level, "PDU dump:");
289                                 odr_dumpBER(yaz_log_file(),
290                                             (char *) r->u.single_ASN1_type->buf,
291                                             r->u.single_ASN1_type->len);
292                             }
293                             else
294                                 yaz_log(log_level, "Decode ILL APDU OK");
295                             if (rr->print)
296                             {
297                                 ill_APDU(rr->print, &ill_apdu, 0,
298                                          "ILL APDU");
299                                 odr_reset(rr->print);
300                             }
301                         }
302                     }
303                 }
304                 if (item_req)
305                 {
306                     yaz_log(log_level, "ILL protocol version = "
307                             ODR_INT_PRINTF,
308                             *item_req->protocol_version_num);
309                 }
310             }
311             if (k)
312             {
313
314                 Z_External *ext = (Z_External *)
315                     odr_malloc(rr->stream, sizeof(*ext));
316                 Z_IUOriginPartToKeep *keep = (Z_IUOriginPartToKeep *)
317                     odr_malloc(rr->stream, sizeof(*keep));
318                 Z_IOTargetPart *targetPart = (Z_IOTargetPart *)
319                     odr_malloc(rr->stream, sizeof(*targetPart));
320
321                 rr->taskPackage = (Z_TaskPackage *)
322                     odr_malloc(rr->stream, sizeof(*rr->taskPackage));
323                 rr->taskPackage->packageType =
324                     odr_oiddup(rr->stream, rr->esr->packageType);
325                 rr->taskPackage->packageName = 0;
326                 rr->taskPackage->userId = 0;
327                 rr->taskPackage->retentionTime = 0;
328                 rr->taskPackage->permissions = 0;
329                 rr->taskPackage->description = 0;
330                 rr->taskPackage->targetReference = (Odr_oct *)
331                     odr_malloc(rr->stream, sizeof(Odr_oct));
332                 rr->taskPackage->targetReference->buf =
333                     (unsigned char *) odr_strdup(rr->stream, "911");
334                 rr->taskPackage->targetReference->len =
335                     rr->taskPackage->targetReference->size =
336                     strlen((char *) (rr->taskPackage->targetReference->buf));
337                 rr->taskPackage->creationDateTime = 0;
338                 rr->taskPackage->taskStatus = odr_intdup(rr->stream, 0);
339                 rr->taskPackage->packageDiagnostics = 0;
340                 rr->taskPackage->taskSpecificParameters = ext;
341
342                 ext->direct_reference =
343                     odr_oiddup(rr->stream, rr->esr->packageType);
344                 ext->indirect_reference = 0;
345                 ext->descriptor = 0;
346                 ext->which = Z_External_itemOrder;
347                 ext->u.itemOrder = (Z_ItemOrder *)
348                     odr_malloc(rr->stream, sizeof(*ext->u.update));
349                 ext->u.itemOrder->which = Z_IOItemOrder_taskPackage;
350                 ext->u.itemOrder->u.taskPackage =  (Z_IOTaskPackage *)
351                     odr_malloc(rr->stream, sizeof(Z_IOTaskPackage));
352                 ext->u.itemOrder->u.taskPackage->originPart = k;
353                 ext->u.itemOrder->u.taskPackage->targetPart = targetPart;
354
355                 if (xml_in_response)
356                     targetPart->itemRequest =
357                         z_ext_record_xml(rr->stream, xml_in_response,
358                                          strlen(xml_in_response));
359                 else
360                     targetPart->itemRequest = 0;
361                     
362                 targetPart->statusOrErrorReport = 0;
363                 targetPart->auxiliaryStatus = 0;
364             }
365         }
366     }
367     else if (rr->esr->taskSpecificParameters->which == Z_External_update)
368     {
369         Z_IUUpdate *up = rr->esr->taskSpecificParameters->u.update;
370         yaz_log(log_level, "Received DB Update");
371         if (up->which == Z_IUUpdate_esRequest)
372         {
373             Z_IUUpdateEsRequest *esRequest = up->u.esRequest;
374             Z_IUOriginPartToKeep *toKeep = esRequest->toKeep;
375             Z_IUSuppliedRecords *notToKeep = esRequest->notToKeep;
376             
377             yaz_log(log_level, "action");
378             if (toKeep->action)
379             {
380                 switch (*toKeep->action)
381                 {
382                 case Z_IUOriginPartToKeep_recordInsert:
383                     yaz_log(log_level, " recordInsert");
384                     break;
385                 case Z_IUOriginPartToKeep_recordReplace:
386                     yaz_log(log_level, " recordReplace");
387                     break;
388                 case Z_IUOriginPartToKeep_recordDelete:
389                     yaz_log(log_level, " recordDelete");
390                     break;
391                 case Z_IUOriginPartToKeep_elementUpdate:
392                     yaz_log(log_level, " elementUpdate");
393                     break;
394                 case Z_IUOriginPartToKeep_specialUpdate:
395                     yaz_log(log_level, " specialUpdate");
396                     break;
397                 default:
398                     yaz_log(log_level, " unknown (" ODR_INT_PRINTF ")",
399                             *toKeep->action);
400                 }
401             }
402             if (toKeep->databaseName)
403             {
404                 yaz_log(log_level, "database: %s", toKeep->databaseName);
405                 if (!strcmp(toKeep->databaseName, "fault"))
406                 {
407                     rr->errcode = YAZ_BIB1_DATABASE_UNAVAILABLE;
408                     rr->errstring = toKeep->databaseName;
409                 }
410                 if (!strcmp(toKeep->databaseName, "accept"))
411                     rr->errcode = -1;
412             }
413             if (toKeep)
414             {
415                 Z_External *ext = (Z_External *)
416                     odr_malloc(rr->stream, sizeof(*ext));
417                 Z_IUOriginPartToKeep *keep = (Z_IUOriginPartToKeep *)
418                     odr_malloc(rr->stream, sizeof(*keep));
419                 Z_IUTargetPart *targetPart = (Z_IUTargetPart *)
420                     odr_malloc(rr->stream, sizeof(*targetPart));
421
422                 rr->taskPackage = (Z_TaskPackage *)
423                     odr_malloc(rr->stream, sizeof(*rr->taskPackage));
424                 rr->taskPackage->packageType =
425                     odr_oiddup(rr->stream, rr->esr->packageType);
426                 rr->taskPackage->packageName = 0;
427                 rr->taskPackage->userId = 0;
428                 rr->taskPackage->retentionTime = 0;
429                 rr->taskPackage->permissions = 0;
430                 rr->taskPackage->description = 0;
431                 rr->taskPackage->targetReference = (Odr_oct *)
432                     odr_malloc(rr->stream, sizeof(Odr_oct));
433                 rr->taskPackage->targetReference->buf =
434                     (unsigned char *) odr_strdup(rr->stream, "123");
435                 rr->taskPackage->targetReference->len =
436                     rr->taskPackage->targetReference->size =
437                     strlen((char *) (rr->taskPackage->targetReference->buf));
438                 rr->taskPackage->creationDateTime = 0;
439                 rr->taskPackage->taskStatus = odr_intdup(rr->stream, 0);
440                 rr->taskPackage->packageDiagnostics = 0;
441                 rr->taskPackage->taskSpecificParameters = ext;
442
443                 ext->direct_reference =
444                     odr_oiddup(rr->stream, rr->esr->packageType);
445                 ext->indirect_reference = 0;
446                 ext->descriptor = 0;
447                 ext->which = Z_External_update;
448                 ext->u.update = (Z_IUUpdate *)
449                     odr_malloc(rr->stream, sizeof(*ext->u.update));
450                 ext->u.update->which = Z_IUUpdate_taskPackage;
451                 ext->u.update->u.taskPackage =  (Z_IUUpdateTaskPackage *)
452                     odr_malloc(rr->stream, sizeof(Z_IUUpdateTaskPackage));
453                 ext->u.update->u.taskPackage->originPart = keep;
454                 ext->u.update->u.taskPackage->targetPart = targetPart;
455
456                 keep->action = odr_intdup(rr->stream, *toKeep->action);
457                 keep->databaseName =
458                     odr_strdup(rr->stream, toKeep->databaseName);
459                 keep->schema = 0;
460                 keep->elementSetName = 0;
461                 keep->actionQualifier = 0;
462
463                 targetPart->updateStatus = odr_intdup(rr->stream, 1);
464                 targetPart->num_globalDiagnostics = 0;
465                 targetPart->globalDiagnostics = (Z_DiagRec **) odr_nullval();
466                 targetPart->num_taskPackageRecords = 1;
467                 targetPart->taskPackageRecords = 
468                     (Z_IUTaskPackageRecordStructure **)
469                     odr_malloc(rr->stream,
470                                sizeof(Z_IUTaskPackageRecordStructure *));
471                 targetPart->taskPackageRecords[0] =
472                     (Z_IUTaskPackageRecordStructure *)
473                     odr_malloc(rr->stream,
474                                sizeof(Z_IUTaskPackageRecordStructure));
475                 
476                 targetPart->taskPackageRecords[0]->which =
477                     Z_IUTaskPackageRecordStructure_record;
478                 targetPart->taskPackageRecords[0]->u.record = 
479                     z_ext_record_sutrs(rr->stream, "test", 4);
480                 targetPart->taskPackageRecords[0]->correlationInfo = 0; 
481                 targetPart->taskPackageRecords[0]->recordStatus =
482                     odr_intdup(rr->stream,
483                                Z_IUTaskPackageRecordStructure_success);  
484                 targetPart->taskPackageRecords[0]->num_supplementalDiagnostics
485                     = 0;
486
487                 targetPart->taskPackageRecords[0]->supplementalDiagnostics = 0;
488             }
489             if (notToKeep)
490             {
491                 int i;
492                 for (i = 0; i < notToKeep->num; i++)
493                 {
494                     Z_External *rec = notToKeep->elements[i]->record;
495
496                     if (rec->direct_reference)
497                     {
498                         char oid_name_str[OID_STR_MAX];
499                         const char *oid_name 
500                             = oid_name = yaz_oid_to_string_buf(
501                                 rec->direct_reference, 0,
502                                 oid_name_str);
503                         if (oid_name)
504                             yaz_log(log_level, "record %d type %s", i,
505                                     oid_name);
506                     }
507                     switch (rec->which)
508                     {
509                     case Z_External_sutrs:
510                         if (rec->u.octet_aligned->len > 170)
511                             yaz_log(log_level, "%d bytes:\n%.168s ...",
512                                     rec->u.sutrs->len,
513                                     rec->u.sutrs->buf);
514                         else
515                             yaz_log(log_level, "%d bytes:\n%s",
516                                     rec->u.sutrs->len,
517                                     rec->u.sutrs->buf);
518                         break;
519                     case Z_External_octet        :
520                         if (rec->u.octet_aligned->len > 170)
521                             yaz_log(log_level, "%d bytes:\n%.168s ...",
522                                     rec->u.octet_aligned->len,
523                                     rec->u.octet_aligned->buf);
524                         else
525                             yaz_log(log_level, "%d bytes\n%s",
526                                     rec->u.octet_aligned->len,
527                                     rec->u.octet_aligned->buf);
528                     }
529                 }
530             }
531         }
532     }
533     return 0;
534 }
535
536 /* result set delete */
537 int ztest_delete(void *handle, bend_delete_rr *rr)
538 {
539     if (rr->num_setnames == 1 && !strcmp(rr->setnames[0], "1"))
540         rr->delete_status = Z_DeleteStatus_success;
541     else
542         rr->delete_status = Z_DeleteStatus_resultSetDidNotExist;
543     return 0;
544 }
545
546 /* Our sort handler really doesn't sort... */
547 int ztest_sort(void *handle, bend_sort_rr *rr)
548 {
549     rr->errcode = 0;
550     rr->sort_status = Z_SortResponse_success;
551     return 0;
552 }
553
554
555 /* present request handler */
556 int ztest_present(void *handle, bend_present_rr *rr)
557 {
558     return 0;
559 }
560
561 /* retrieval of a single record (present, and piggy back search) */
562 int ztest_fetch(void *handle, bend_fetch_rr *r)
563 {
564     char *cp;
565     const Odr_oid *oid = r->request_format;
566
567     r->last_in_set = 0;
568     r->basename = "Default";
569     r->output_format = r->request_format;
570
571     if (!oid || yaz_oid_is_iso2709(oid))
572     {
573         cp = dummy_marc_record(r->number, r->stream);
574         if (!cp)
575         {
576             r->errcode = YAZ_BIB1_PRESENT_REQUEST_OUT_OF_RANGE;
577             return 0;
578         }
579         else
580         {
581             r->len = strlen(cp);
582             r->record = cp;
583             r->output_format = odr_oiddup(r->stream, yaz_oid_recsyn_usmarc);
584         }
585     }
586     else if (!oid_oidcmp(oid, yaz_oid_recsyn_opac))
587     {
588         cp = dummy_marc_record(r->number, r->stream);
589         if (!cp)
590         {
591             r->errcode = YAZ_BIB1_PRESENT_REQUEST_OUT_OF_RANGE;
592             return 0;
593         }
594         r->record = (char *) dummy_opac(r->number, r->stream, cp);
595         r->len = -1;
596     }
597     else if (!oid_oidcmp(oid, yaz_oid_recsyn_sutrs))
598     {
599         /* this section returns a small record */
600         char buf[100];
601         
602         sprintf(buf, "This is dummy SUTRS record number %d\n", r->number);
603
604         r->len = strlen(buf);
605         r->record = (char *) odr_malloc(r->stream, r->len+1);
606         strcpy(r->record, buf);
607     }
608     else if (!oid_oidcmp(oid, yaz_oid_recsyn_grs_1))
609     {
610         r->len = -1;
611         r->record = (char*) dummy_grs_record(r->number, r->stream);
612         if (!r->record)
613         {
614             r->errcode = YAZ_BIB1_PRESENT_REQUEST_OUT_OF_RANGE;
615             return 0;
616         }
617     }
618     else if (!oid_oidcmp(oid, yaz_oid_recsyn_postscript))
619     {
620         char fname[20];
621         FILE *f;
622         long size;
623
624         sprintf(fname, "part.%d.ps", r->number);
625         f = fopen(fname, "rb");
626         if (!f)
627         {
628             r->errcode = YAZ_BIB1_PRESENT_REQUEST_OUT_OF_RANGE;
629             return 0;
630         }
631         fseek(f, 0L, SEEK_END);
632         size = ftell(f);
633         if (size <= 0 || size >= 5000000)
634         {
635             r->errcode = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
636         }
637         fseek(f, 0L, SEEK_SET);
638         r->record = (char*) odr_malloc(r->stream, size);
639         r->len = size;
640         if (fread(r->record, size, 1, f) != 1)
641         {
642             r->errcode = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
643         }
644         fclose(f);
645     }
646     else if (!oid_oidcmp(oid, yaz_oid_recsyn_xml))
647     {
648         if ((cp = dummy_xml_record(r->number, r->stream)))
649         {
650             r->len = strlen(cp);
651             r->record = cp;
652         }
653         else 
654         {
655             r->errcode = YAZ_BIB1_PRESENT_REQUEST_OUT_OF_RANGE;
656             r->surrogate_flag = 1;
657             return 0;
658         }
659     }
660     else
661     {
662         char buf[OID_STR_MAX];
663         r->errcode = YAZ_BIB1_RECORD_SYNTAX_UNSUPP;
664         r->errstring = odr_strdup(r->stream, oid_oid_to_dotstring(oid, buf));
665         return 0;
666     }
667     r->errcode = 0;
668     return 0;
669 }
670
671 /*
672  * silly dummy-scan what reads words from a file.
673  */
674 int ztest_scan(void *handle, bend_scan_rr *q)
675 {
676     static FILE *f = 0;
677     static struct scan_entry list[200];
678     static char entries[200][80];
679     int hits[200];
680     char term[80], *p;
681     int i, pos;
682     int term_position_req = q->term_position;
683     int num_entries_req = q->num_entries;
684
685     /* Throw Database unavailable if other than Default or Slow */
686     if (!yaz_matchstr(q->basenames[0], "Default"))
687         ;  /* Default is OK in our test */
688     else if (check_slow(q->basenames[0], 0 /* no assoc for scan */))
689         ;
690     else
691     {
692         q->errcode = YAZ_BIB1_DATABASE_UNAVAILABLE;
693         q->errstring = q->basenames[0];
694         return 0;
695     }
696
697     q->errcode = 0;
698     q->errstring = 0;
699     q->entries = list;
700     q->status = BEND_SCAN_SUCCESS;
701     if (!f && !(f = fopen("dummy-words", "r")))
702     {
703         perror("dummy-words");
704         exit(1);
705     }
706     if (q->num_entries > 200)
707     {
708         q->errcode = YAZ_BIB1_RESOURCES_EXHAUSTED_NO_RESULTS_AVAILABLE;
709         return 0;
710     }
711     if (q->term)
712     {
713         int len;
714         if (q->term->term->which != Z_Term_general)
715         {
716             q->errcode = YAZ_BIB1_TERM_TYPE_UNSUPP;
717             return 0;
718         }
719         if (*q->step_size != 0)
720         {
721             q->errcode = YAZ_BIB1_ONLY_ZERO_STEP_SIZE_SUPPORTED_FOR_SCAN;
722             return 0;
723         }
724         len = q->term->term->u.general->len;
725         if (len >= sizeof(term))
726             len = sizeof(term)-1;
727         memcpy(term, q->term->term->u.general->buf, len);
728         term[len] = '\0';
729     }
730     else if (q->scanClause)
731     {
732         strncpy(term, q->scanClause, sizeof(term)-1);
733         term[sizeof(term)-1] = '\0';
734     }
735     else
736         strcpy(term, "0");
737
738     for (p = term; *p; p++)
739         if (islower(*(unsigned char *) p))
740             *p = toupper(*p);
741
742     fseek(f, 0, SEEK_SET);
743     q->num_entries = 0;
744
745     for (i = 0, pos = 0; fscanf(f, " %79[^:]:%d", entries[pos], &hits[pos]) == 2;
746          i++, pos < 199 ? pos++ : (pos = 0))
747     {
748         if (!q->num_entries && strcmp(entries[pos], term) >= 0) /* s-point fnd */
749         {
750             if ((q->term_position = term_position_req) > i + 1)
751             {
752                 q->term_position = i + 1;
753                 q->status = BEND_SCAN_PARTIAL;
754             }
755             for (; q->num_entries < q->term_position; q->num_entries++)
756             {
757                 int po;
758
759                 po = pos - q->term_position + q->num_entries+1; /* find pos */
760                 if (po < 0)
761                     po += 200;
762
763                 if (!strcmp(term, "SD") && q->num_entries == 2)
764                 {
765                     list[q->num_entries].term = entries[pos];
766                     list[q->num_entries].occurrences = -1;
767                     list[q->num_entries].errcode =
768                         YAZ_BIB1_SCAN_UNSUPP_VALUE_OF_POSITION_IN_RESPONSE;
769                     list[q->num_entries].errstring = "SD for Scan Term";
770                 }
771                 else
772                 {
773                     list[q->num_entries].term = entries[po];
774                     list[q->num_entries].occurrences = hits[po];
775                 }
776             }
777         }
778         else if (q->num_entries)
779         {
780             list[q->num_entries].term = entries[pos];
781             list[q->num_entries].occurrences = hits[pos];
782             q->num_entries++;
783         }
784         if (q->num_entries >= num_entries_req)
785             break;
786     }
787     if (feof(f))
788         q->status = BEND_SCAN_PARTIAL;
789     return 0;
790 }
791
792 int ztest_explain(void *handle, bend_explain_rr *rr)
793 {
794     if (rr->database && !strcmp(rr->database, "Default"))
795     {
796         rr->explain_buf = "<explain>\n"
797             "\t<serverInfo>\n"
798             "\t\t<host>localhost</host>\n"
799             "\t\t<port>210</port>\n"
800             "\t</serverInfo>\n"
801             "</explain>\n";
802     }
803     return 0;
804 }
805
806 int ztest_update(void *handle, bend_update_rr *rr)
807 {
808     rr->operation_status = "success";
809     return 0;
810 }
811
812 bend_initresult *bend_init(bend_initrequest *q)
813 {
814     bend_initresult *r = (bend_initresult *)
815         odr_malloc(q->stream, sizeof(*r));
816     int *counter = (int *) xmalloc(sizeof(int));
817
818     if (!log_level_set)
819     {
820         log_level=yaz_log_module_level("ztest");
821         log_level_set=1;
822     }
823
824     *counter = 0;
825     r->errcode = 0;
826     r->errstring = 0;
827     r->handle = counter;         /* user handle, in this case a simple int */
828     q->bend_sort = ztest_sort;              /* register sort handler */
829     q->bend_search = ztest_search;          /* register search handler */
830     q->bend_present = ztest_present;        /* register present handle */
831     q->bend_esrequest = ztest_esrequest;
832     q->bend_delete = ztest_delete;
833     q->bend_fetch = ztest_fetch;
834     q->bend_scan = ztest_scan;
835 #if 0
836     q->bend_explain = ztest_explain;
837 #endif
838     q->bend_srw_scan = ztest_scan;
839     q->bend_srw_update = ztest_update;
840
841     q->query_charset = "ISO-8859-1";
842     q->records_in_same_charset = 0;
843
844     return r;
845 }
846
847 void bend_close(void *handle)
848 {
849     xfree(handle);              /* release our user-defined handle */
850     return;
851 }
852
853 int main(int argc, char **argv)
854 {
855     return statserv_main(argc, argv, bend_init, bend_close);
856 }
857 /*
858  * Local variables:
859  * c-basic-offset: 4
860  * c-file-style: "Stroustrup"
861  * indent-tabs-mode: nil
862  * End:
863  * vim: shiftwidth=4 tabstop=8 expandtab
864  */
865