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