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