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