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