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