yaz-ztest returns dummy OPAC records.
[yaz-moved-to-github.git] / ztest / ztest.c
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) 1995-2008 Index Data
3  * See the file LICENSE for details.
4  */
5
6 /*
7  * Demonstration of simple 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 static int log_level=0;
23 static int log_level_set=0;
24
25 Z_GenericRecord *dummy_grs_record (int num, ODR o);
26 char *dummy_marc_record (int num, ODR odr);
27 char *dummy_xml_record (int num, ODR odr);
28
29 int ztest_search(void *handle, bend_search_rr *rr);
30 int ztest_sort(void *handle, bend_sort_rr *rr);
31 int ztest_present(void *handle, bend_present_rr *rr);
32 int ztest_esrequest(void *handle, bend_esrequest_rr *rr);
33 int ztest_delete(void *handle, bend_delete_rr *rr);
34
35 /** \fn get_term_hit
36     \brief use term value as hit count 
37    
38     Traverse RPN tree 'in order' and use term value as hit count.
39     Only terms  that looks a numeric is used.. Returns -1 if
40     no sub tree has a hit count term
41 */
42 static int get_term_hit(Z_RPNStructure *s)
43 {
44     int h = -1;
45     switch(s->which)
46     {
47     case Z_RPNStructure_simple:
48         if (s->u.simple->which == Z_Operand_APT)
49         {
50             Z_AttributesPlusTerm *apt = s->u.simple->u.attributesPlusTerm;
51             if (apt->term->which == Z_Term_general)
52             {
53                 Odr_oct *oct = apt->term->u.general;
54                 if (oct->len > 0 && oct->buf[0] >= '0' && oct->buf[0] <= '9')
55                     h = atoi_n((const char *) oct->buf, oct->len);
56             }
57         }
58         break;
59     case Z_RPNStructure_complex:
60         h = get_term_hit(s->u.complex->s1);
61         if (h == -1)
62             h = get_term_hit(s->u.complex->s2);
63         break;
64     }
65     return h;
66 }
67
68 /** \fn get_hit_count
69     \brief gets hit count for numeric terms in RPN queries
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 = 23;
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 = 109;
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: %d", *rr->esr->waitAction);
138
139
140     yaz_log(log_level, "function: %d", *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: %d", *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 = %d",
258                              *item_req->protocol_version_num);
259                 }
260             }
261             if (k)
262             {
263
264                 Z_External *ext = (Z_External *)
265                     odr_malloc (rr->stream, sizeof(*ext));
266                 Z_IUOriginPartToKeep *keep = (Z_IUOriginPartToKeep *)
267                     odr_malloc (rr->stream, sizeof(*keep));
268                 Z_IOTargetPart *targetPart = (Z_IOTargetPart *)
269                     odr_malloc (rr->stream, sizeof(*targetPart));
270
271                 rr->taskPackage = (Z_TaskPackage *)
272                     odr_malloc (rr->stream, sizeof(*rr->taskPackage));
273                 rr->taskPackage->packageType =
274                     odr_oiddup (rr->stream, rr->esr->packageType);
275                 rr->taskPackage->packageName = 0;
276                 rr->taskPackage->userId = 0;
277                 rr->taskPackage->retentionTime = 0;
278                 rr->taskPackage->permissions = 0;
279                 rr->taskPackage->description = 0;
280                 rr->taskPackage->targetReference = (Odr_oct *)
281                     odr_malloc (rr->stream, sizeof(Odr_oct));
282                 rr->taskPackage->targetReference->buf =
283                     (unsigned char *) odr_strdup (rr->stream, "911");
284                 rr->taskPackage->targetReference->len =
285                     rr->taskPackage->targetReference->size =
286                     strlen((char *) (rr->taskPackage->targetReference->buf));
287                 rr->taskPackage->creationDateTime = 0;
288                 rr->taskPackage->taskStatus = odr_intdup(rr->stream, 0);
289                 rr->taskPackage->packageDiagnostics = 0;
290                 rr->taskPackage->taskSpecificParameters = ext;
291
292                 ext->direct_reference =
293                     odr_oiddup (rr->stream, rr->esr->packageType);
294                 ext->indirect_reference = 0;
295                 ext->descriptor = 0;
296                 ext->which = Z_External_itemOrder;
297                 ext->u.itemOrder = (Z_ItemOrder *)
298                     odr_malloc (rr->stream, sizeof(*ext->u.update));
299                 ext->u.itemOrder->which = Z_IOItemOrder_taskPackage;
300                 ext->u.itemOrder->u.taskPackage =  (Z_IOTaskPackage *)
301                     odr_malloc (rr->stream, sizeof(Z_IOTaskPackage));
302                 ext->u.itemOrder->u.taskPackage->originPart = k;
303                 ext->u.itemOrder->u.taskPackage->targetPart = targetPart;
304
305                 if (xml_in_response)
306                     targetPart->itemRequest =
307                         z_ext_record_xml(rr->stream, xml_in_response,
308                                          strlen(xml_in_response));
309                 else
310                     targetPart->itemRequest = 0;
311                     
312                 targetPart->statusOrErrorReport = 0;
313                 targetPart->auxiliaryStatus = 0;
314             }
315         }
316     }
317     else if (rr->esr->taskSpecificParameters->which == Z_External_update)
318     {
319         Z_IUUpdate *up = rr->esr->taskSpecificParameters->u.update;
320         yaz_log (log_level, "Received DB Update");
321         if (up->which == Z_IUUpdate_esRequest)
322         {
323             Z_IUUpdateEsRequest *esRequest = up->u.esRequest;
324             Z_IUOriginPartToKeep *toKeep = esRequest->toKeep;
325             Z_IUSuppliedRecords *notToKeep = esRequest->notToKeep;
326             
327             yaz_log (log_level, "action");
328             if (toKeep->action)
329             {
330                 switch (*toKeep->action)
331                 {
332                 case Z_IUOriginPartToKeep_recordInsert:
333                     yaz_log (log_level, " recordInsert");
334                     break;
335                 case Z_IUOriginPartToKeep_recordReplace:
336                     yaz_log (log_level, " recordReplace");
337                     break;
338                 case Z_IUOriginPartToKeep_recordDelete:
339                     yaz_log (log_level, " recordDelete");
340                     break;
341                 case Z_IUOriginPartToKeep_elementUpdate:
342                     yaz_log (log_level, " elementUpdate");
343                     break;
344                 case Z_IUOriginPartToKeep_specialUpdate:
345                     yaz_log (log_level, " specialUpdate");
346                     break;
347                 default:
348                     yaz_log (log_level, " unknown (%d)", *toKeep->action);
349                 }
350             }
351             if (toKeep->databaseName)
352             {
353                 yaz_log (log_level, "database: %s", toKeep->databaseName);
354                 if (!strcmp(toKeep->databaseName, "fault"))
355                 {
356                     rr->errcode = 109;
357                     rr->errstring = toKeep->databaseName;
358                 }
359                 if (!strcmp(toKeep->databaseName, "accept"))
360                     rr->errcode = -1;
361             }
362             if (toKeep)
363             {
364                 Z_External *ext = (Z_External *)
365                     odr_malloc (rr->stream, sizeof(*ext));
366                 Z_IUOriginPartToKeep *keep = (Z_IUOriginPartToKeep *)
367                     odr_malloc (rr->stream, sizeof(*keep));
368                 Z_IUTargetPart *targetPart = (Z_IUTargetPart *)
369                     odr_malloc (rr->stream, sizeof(*targetPart));
370
371                 rr->taskPackage = (Z_TaskPackage *)
372                     odr_malloc (rr->stream, sizeof(*rr->taskPackage));
373                 rr->taskPackage->packageType =
374                     odr_oiddup (rr->stream, rr->esr->packageType);
375                 rr->taskPackage->packageName = 0;
376                 rr->taskPackage->userId = 0;
377                 rr->taskPackage->retentionTime = 0;
378                 rr->taskPackage->permissions = 0;
379                 rr->taskPackage->description = 0;
380                 rr->taskPackage->targetReference = (Odr_oct *)
381                     odr_malloc (rr->stream, sizeof(Odr_oct));
382                 rr->taskPackage->targetReference->buf =
383                     (unsigned char *) odr_strdup (rr->stream, "123");
384                 rr->taskPackage->targetReference->len =
385                     rr->taskPackage->targetReference->size =
386                     strlen((char *) (rr->taskPackage->targetReference->buf));
387                 rr->taskPackage->creationDateTime = 0;
388                 rr->taskPackage->taskStatus = odr_intdup(rr->stream, 0);
389                 rr->taskPackage->packageDiagnostics = 0;
390                 rr->taskPackage->taskSpecificParameters = ext;
391
392                 ext->direct_reference =
393                     odr_oiddup (rr->stream, rr->esr->packageType);
394                 ext->indirect_reference = 0;
395                 ext->descriptor = 0;
396                 ext->which = Z_External_update;
397                 ext->u.update = (Z_IUUpdate *)
398                     odr_malloc (rr->stream, sizeof(*ext->u.update));
399                 ext->u.update->which = Z_IUUpdate_taskPackage;
400                 ext->u.update->u.taskPackage =  (Z_IUUpdateTaskPackage *)
401                     odr_malloc (rr->stream, sizeof(Z_IUUpdateTaskPackage));
402                 ext->u.update->u.taskPackage->originPart = keep;
403                 ext->u.update->u.taskPackage->targetPart = targetPart;
404
405                 keep->action = (int *) odr_malloc (rr->stream, sizeof(int));
406                 *keep->action = *toKeep->action;
407                 keep->databaseName =
408                     odr_strdup (rr->stream, toKeep->databaseName);
409                 keep->schema = 0;
410                 keep->elementSetName = 0;
411                 keep->actionQualifier = 0;
412
413                 targetPart->updateStatus = odr_intdup (rr->stream, 1);
414                 targetPart->num_globalDiagnostics = 0;
415                 targetPart->globalDiagnostics = (Z_DiagRec **) odr_nullval();
416                 targetPart->num_taskPackageRecords = 1;
417                 targetPart->taskPackageRecords = 
418                     (Z_IUTaskPackageRecordStructure **)
419                     odr_malloc (rr->stream,
420                                 sizeof(Z_IUTaskPackageRecordStructure *));
421                 targetPart->taskPackageRecords[0] =
422                     (Z_IUTaskPackageRecordStructure *)
423                     odr_malloc (rr->stream,
424                                 sizeof(Z_IUTaskPackageRecordStructure));
425                 
426                 targetPart->taskPackageRecords[0]->which =
427                     Z_IUTaskPackageRecordStructure_record;
428                 targetPart->taskPackageRecords[0]->u.record = 
429                     z_ext_record_sutrs(rr->stream, "test", 4);
430                 targetPart->taskPackageRecords[0]->correlationInfo = 0; 
431                 targetPart->taskPackageRecords[0]->recordStatus =
432                     odr_intdup (rr->stream,
433                                 Z_IUTaskPackageRecordStructure_success);  
434                 targetPart->taskPackageRecords[0]->num_supplementalDiagnostics
435                     = 0;
436
437                 targetPart->taskPackageRecords[0]->supplementalDiagnostics = 0;
438             }
439             if (notToKeep)
440             {
441                 int i;
442                 for (i = 0; i < notToKeep->num; i++)
443                 {
444                     Z_External *rec = notToKeep->elements[i]->record;
445
446                     if (rec->direct_reference)
447                     {
448                         char oid_name_str[OID_STR_MAX];
449                         const char *oid_name 
450                             = oid_name = yaz_oid_to_string_buf(
451                                 rec->direct_reference, 0,
452                                 oid_name_str);
453                         if (oid_name)
454                             yaz_log (log_level, "record %d type %s", i,
455                                      oid_name);
456                     }
457                     switch (rec->which)
458                     {
459                     case Z_External_sutrs:
460                         if (rec->u.octet_aligned->len > 170)
461                             yaz_log (log_level, "%d bytes:\n%.168s ...",
462                                      rec->u.sutrs->len,
463                                      rec->u.sutrs->buf);
464                         else
465                             yaz_log (log_level, "%d bytes:\n%s",
466                                      rec->u.sutrs->len,
467                                      rec->u.sutrs->buf);
468                         break;
469                     case Z_External_octet        :
470                         if (rec->u.octet_aligned->len > 170)
471                             yaz_log (log_level, "%d bytes:\n%.168s ...",
472                                      rec->u.octet_aligned->len,
473                                      rec->u.octet_aligned->buf);
474                         else
475                             yaz_log (log_level, "%d bytes\n%s",
476                                      rec->u.octet_aligned->len,
477                                      rec->u.octet_aligned->buf);
478                     }
479                 }
480             }
481         }
482     }
483     return 0;
484 }
485
486 /* result set delete */
487 int ztest_delete (void *handle, bend_delete_rr *rr)
488 {
489     if (rr->num_setnames == 1 && !strcmp (rr->setnames[0], "1"))
490         rr->delete_status = Z_DeleteStatus_success;
491     else
492         rr->delete_status = Z_DeleteStatus_resultSetDidNotExist;
493     return 0;
494 }
495
496 /* Our sort handler really doesn't sort... */
497 int ztest_sort (void *handle, bend_sort_rr *rr)
498 {
499     rr->errcode = 0;
500     rr->sort_status = Z_SortResponse_success;
501     return 0;
502 }
503
504
505 /* present request handler */
506 int ztest_present (void *handle, bend_present_rr *rr)
507 {
508     return 0;
509 }
510
511 /* retrieval of a single record (present, and piggy back search) */
512 int ztest_fetch(void *handle, bend_fetch_rr *r)
513 {
514     char *cp;
515     const Odr_oid *oid = r->request_format;
516
517     r->last_in_set = 0;
518     r->basename = "Default";
519     r->output_format = r->request_format;
520
521     if (!oid || yaz_oid_is_iso2709(oid))
522     {
523         cp = dummy_marc_record(r->number, r->stream);
524         if (!cp)
525         {
526             r->errcode = YAZ_BIB1_PRESENT_REQUEST_OUT_OF_RANGE;
527         }
528         else
529         {
530             r->len = strlen(cp);
531             r->record = cp;
532             r->output_format = odr_oiddup(r->stream, yaz_oid_recsyn_usmarc);
533         }
534     }
535     else if (!oid_oidcmp(oid, yaz_oid_recsyn_opac))
536     {
537         Z_OPACRecord *rec;
538         int i;
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         rec = odr_malloc(r->stream, sizeof(*rec));
546         rec->bibliographicRecord =
547             z_ext_record_usmarc(r->stream, cp, strlen(cp));
548         rec->num_holdingsData = 1;
549         rec->holdingsData = odr_malloc(r->stream, sizeof(*rec->holdingsData));
550         for (i = 0; i < rec->num_holdingsData; i++)
551         {
552             Z_HoldingsRecord *hr = odr_malloc(r->stream, sizeof(*hr));
553             Z_HoldingsAndCircData *hc = odr_malloc(r->stream, sizeof(*hc));
554
555             rec->holdingsData[i] = hr;
556             hr->which = Z_HoldingsRecord_holdingsAndCirc;
557             hr->u.holdingsAndCirc = hc;
558             
559             hc->typeOfRecord = odr_strdup(r->stream, "x");
560             hc->typeOfRecord[0] = cp[5]; /* LDR 6 */
561
562             hc->encodingLevel = odr_strdup(r->stream, "x");
563             hc->encodingLevel[0] = cp[16]; /* LDR 17 */
564
565             hc->format = 0; /* OPT */
566             hc->receiptAcqStatus = 0; /* OPT */
567             hc->generalRetention = 0; /* OPT */
568             hc->completeness = 0; /* OPT */
569             hc->dateOfReport = 0; /* OPT */
570             hc->nucCode = 0; /* OPT */
571             hc->localLocation = 0; /* OPT */
572             hc->shelvingLocation = 0; /* OPT */
573             hc->callNumber = 0; /* OPT */
574             hc->shelvingData = 0; /* OPT */
575             hc->copyNumber = 0; /* OPT */
576             hc->publicNote = 0; /* OPT */
577             hc->reproductionNote = 0; /* OPT */
578             hc->termsUseRepro = 0; /* OPT */
579             hc->enumAndChron = 0; /* OPT */
580
581             hc->num_volumes = 0;
582             hc->volumes = 0;
583
584             hc->num_circulationData = 0;
585             hc->circulationData = 0;
586         }
587
588         r->len = -1;
589         r->record = (char*) rec;
590     }
591     else if (!oid_oidcmp(oid, yaz_oid_recsyn_sutrs))
592     {
593         /* this section returns a small record */
594         char buf[100];
595         
596         sprintf(buf, "This is dummy SUTRS record number %d\n", r->number);
597
598         r->len = strlen(buf);
599         r->record = (char *) odr_malloc (r->stream, r->len+1);
600         strcpy(r->record, buf);
601     }
602     else if (!oid_oidcmp(oid, yaz_oid_recsyn_grs_1))
603     {
604         r->len = -1;
605         r->record = (char*) dummy_grs_record(r->number, r->stream);
606         if (!r->record)
607         {
608             r->errcode = YAZ_BIB1_PRESENT_REQUEST_OUT_OF_RANGE;
609             return 0;
610         }
611     }
612     else if (!oid_oidcmp(oid, yaz_oid_recsyn_postscript))
613     {
614         char fname[20];
615         FILE *f;
616         long size;
617
618         sprintf (fname, "part.%d.ps", r->number);
619         f = fopen(fname, "rb");
620         if (!f)
621         {
622             r->errcode = YAZ_BIB1_PRESENT_REQUEST_OUT_OF_RANGE;
623             return 0;
624         }
625         fseek (f, 0L, SEEK_END);
626         size = ftell (f);
627         if (size <= 0 || size >= 5000000)
628         {
629             r->errcode = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
630             return 0;
631         }
632         fseek (f, 0L, SEEK_SET);
633         r->record = (char*) odr_malloc (r->stream, size);
634         r->len = size;
635         fread (r->record, size, 1, f);
636         fclose (f);
637     }
638     else if (!oid_oidcmp(oid, yaz_oid_recsyn_xml))
639     {
640         if ((cp = dummy_xml_record (r->number, r->stream)))
641         {
642             r->len = strlen(cp);
643             r->record = cp;
644         }
645         else 
646         {
647             r->errcode = YAZ_BIB1_PRESENT_REQUEST_OUT_OF_RANGE;
648             r->surrogate_flag = 1;
649             return 0;
650         }
651     }
652     else
653     {
654         char buf[OID_STR_MAX];
655         r->errcode = YAZ_BIB1_RECORD_SYNTAX_UNSUPP;
656         r->errstring = odr_strdup(r->stream, oid_oid_to_dotstring(oid, buf));
657         return 0;
658     }
659     r->errcode = 0;
660     return 0;
661 }
662
663 /*
664  * silly dummy-scan what reads words from a file.
665  */
666 int ztest_scan(void *handle, bend_scan_rr *q)
667 {
668     static FILE *f = 0;
669     static struct scan_entry list[200];
670     static char entries[200][80];
671     int hits[200];
672     char term[80], *p;
673     int i, pos;
674     int term_position_req = q->term_position;
675     int num_entries_req = q->num_entries;
676
677     /* Throw Database unavailable if other than Default or Slow */
678     if (!yaz_matchstr (q->basenames[0], "Default"))
679         ;  /* Default is OK in our test */
680     else if(!yaz_matchstr (q->basenames[0], "Slow"))
681     {
682 #if HAVE_UNISTD_H
683         sleep(3);
684 #endif
685         ;
686     }
687     else
688     {
689         q->errcode = 109;
690         q->errstring = q->basenames[0];
691         return 0;
692     }
693
694     q->errcode = 0;
695     q->errstring = 0;
696     q->entries = list;
697     q->status = BEND_SCAN_SUCCESS;
698     if (!f && !(f = fopen("dummy-words", "r")))
699     {
700         perror("dummy-words");
701         exit(1);
702     }
703     if (q->num_entries > 200)
704     {
705         q->errcode = 31;
706         return 0;
707     }
708     if (q->term)
709     {
710         int len;
711         if (q->term->term->which != Z_Term_general)
712         {
713             q->errcode = 229; /* unsupported term type */
714             return 0;
715         }
716         if (*q->step_size != 0)
717         {
718             q->errcode = 205; /*Only zero step size supported for Scan */
719             return 0;
720         }
721         len = q->term->term->u.general->len;
722         if (len >= sizeof(term))
723             len = sizeof(term)-1;
724         memcpy(term, q->term->term->u.general->buf, len);
725         term[len] = '\0';
726     }
727     else if (q->scanClause)
728     {
729         strncpy(term, q->scanClause, sizeof(term)-1);
730         term[sizeof(term)-1] = '\0';
731     }
732     else
733         strcpy(term, "0");
734
735     for (p = term; *p; p++)
736         if (islower(*(unsigned char *) p))
737             *p = toupper(*p);
738
739     fseek(f, 0, SEEK_SET);
740     q->num_entries = 0;
741
742     for (i = 0, pos = 0; fscanf(f, " %79[^:]:%d", entries[pos], &hits[pos]) == 2;
743         i++, pos < 199 ? pos++ : (pos = 0))
744     {
745         if (!q->num_entries && strcmp(entries[pos], term) >= 0) /* s-point fnd */
746         {
747             if ((q->term_position = term_position_req) > i + 1)
748             {
749                 q->term_position = i + 1;
750                 q->status = BEND_SCAN_PARTIAL;
751             }
752             for (; q->num_entries < q->term_position; q->num_entries++)
753             {
754                 int po;
755
756                 po = pos - q->term_position + q->num_entries+1; /* find pos */
757                 if (po < 0)
758                     po += 200;
759
760                 if (!strcmp (term, "SD") && q->num_entries == 2)
761                 {
762                     list[q->num_entries].term = entries[pos];
763                     list[q->num_entries].occurrences = -1;
764                     list[q->num_entries].errcode = 233;
765                     list[q->num_entries].errstring = "SD for Scan Term";
766                 }
767                 else
768                 {
769                     list[q->num_entries].term = entries[po];
770                     list[q->num_entries].occurrences = hits[po];
771                 }
772             }
773         }
774         else if (q->num_entries)
775         {
776             list[q->num_entries].term = entries[pos];
777             list[q->num_entries].occurrences = hits[pos];
778             q->num_entries++;
779         }
780         if (q->num_entries >= num_entries_req)
781             break;
782     }
783     if (feof(f))
784         q->status = BEND_SCAN_PARTIAL;
785     return 0;
786 }
787
788 int ztest_explain(void *handle, bend_explain_rr *rr)
789 {
790     if (rr->database && !strcmp(rr->database, "Default"))
791     {
792         rr->explain_buf = "<explain>\n"
793             "\t<serverInfo>\n"
794             "\t\t<host>localhost</host>\n"
795             "\t\t<port>210</port>\n"
796             "\t</serverInfo>\n"
797             "</explain>\n";
798     }
799     return 0;
800 }
801
802 int ztest_update(void *handle, bend_update_rr *rr)
803 {
804     rr->operation_status = "success";
805     return 0;
806 }
807
808 bend_initresult *bend_init(bend_initrequest *q)
809 {
810     bend_initresult *r = (bend_initresult *)
811         odr_malloc (q->stream, sizeof(*r));
812     int *counter = (int *) xmalloc (sizeof(int));
813
814     if (!log_level_set)
815     {
816         log_level=yaz_log_module_level("ztest");
817         log_level_set=1;
818     }
819
820     *counter = 0;
821     r->errcode = 0;
822     r->errstring = 0;
823     r->handle = counter;         /* user handle, in this case a simple int */
824     q->bend_sort = ztest_sort;              /* register sort handler */
825     q->bend_search = ztest_search;          /* register search handler */
826     q->bend_present = ztest_present;        /* register present handle */
827     q->bend_esrequest = ztest_esrequest;
828     q->bend_delete = ztest_delete;
829     q->bend_fetch = ztest_fetch;
830     q->bend_scan = ztest_scan;
831 #if 0
832     q->bend_explain = ztest_explain;
833 #endif
834     q->bend_srw_scan = ztest_scan;
835     q->bend_srw_update = ztest_update;
836
837     q->query_charset = "ISO-8859-1";
838     q->records_in_same_charset = 0;
839
840     return r;
841 }
842
843 void bend_close(void *handle)
844 {
845     xfree (handle);              /* release our user-defined handle */
846     return;
847 }
848
849 int main(int argc, char **argv)
850 {
851     return statserv_main(argc, argv, bend_init, bend_close);
852 }
853 /*
854  * Local variables:
855  * c-basic-offset: 4
856  * indent-tabs-mode: nil
857  * End:
858  * vim: shiftwidth=4 tabstop=8 expandtab
859  */
860