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