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